2

I have a printer named "Teacher's Lounge Printer" (note the apostrophe).

When executing the following command at a standard Windows 7 Command Prompt, "Error 0x8004103A Invalid object path" occurs.

cscript "%WINDIR%\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs" -d -p "Teacher's Lounge Printer"

If I rename the printer to "Teachers Lounge Printer" (without the apostrophe), the command without the apostrophe will execute successfully.

cscript "%WINDIR%\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs" -d -p "Teachers Lounge Printer"

I feel like I've tried every combination of double-quotes, single-quotes, and I've even attempted escape characters.

Eventually, this will make it into a batch script I'm writing, but I need to get the individual command working properly, first.

I know I'm doing something wrong, and any help is appreciated.

Squashman
  • 13,649
  • 5
  • 27
  • 36
wmbell
  • 23
  • 3

1 Answers1

0

Microsoft's prnmngr.vbs script uses WMI to provide the underlying functionality.

WMI accepts quoted strings using either single ' quotes, or double " quotes. Unfortunately the developer of prnmngr.vbs chose to use single quotes in two places, which causes problems when the printer name contains single quotes (apostrophe).

I haven't figured out a way to escape the ' in a prnmnger.vbs parameter in a way that WMI will accept. But I have figured out how to hack (debug) the Microsoft prnmngr.vbs code to eliminate the problem.

On my machine, there are two identical offending lines at line numbers 462 and 818.

        set oPrinter = oService.Get("Win32_Printer.DeviceID='" & strPrinter & "'")

The bug can be fixed by switching to using double quotes in the WMI query string. Since the double quote literals are within a VBS quoted string, the quotes must be escaped as "".

        set oPrinter = oService.Get("Win32_Printer.DeviceID=""" & strPrinter & """")

Rather than modifying the original Microsoft file, you may want to copy the file into a different folder, and make the changes there, and run that hacked version instead of the Microsoft original.

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • I noticed that `prnmngr.vbs` deletes printers with apostrophes `'` properly, when using the `-x` parameter, which deletes all printers. I'll have a look at the VBS file, but I'm curious as to how the code differs. – wmbell Mar 29 '18 at 13:31
  • @wmbell - Deleting a specific printer requires a WMI query with a where clause that specifies the name of the printer as a quoted value. The query returns the specified printer object. Deleting all printers simply iterates all available printers without requiring a where clause, so no quoted value. – dbenham Mar 29 '18 at 14:04
  • Looks like the `-x` parameter uses a FOR EACH loop on each `Win32_Printer` instance, instead of querying the printers by name. This would explain why the `-x` parameter works and the `-d -p` parameters don't. – wmbell Mar 29 '18 at 14:16
  • On a side note... It also appears that this exact issue exists within several of the other built-in printing VBS files. – wmbell Mar 29 '18 at 14:26
  • @dbenham I tried your hack. Still get the same error. I have a remote store that thought they were being cute by renaming their printer "DON'T USE". Can't delete it. – BigRedEO Jul 24 '19 at 14:15