Unable to set the default printer on a remote machine using prnmngr.vbs

1.3k Views Asked by At

I am using WMI to connect to my lab machine as a domain admin. I then run this command line to create a printer:

cscript C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -a -p Test002 -m "Canon Inkjet iP100 series" -r FAKE002

That works fine.

I then run this command line to set the printer as the default:

cscript C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -t -p Test002

That doesn't work at all.

Some pertinent details:

  • Both command lines are executed by the same method
  • The second command line works fine if I run it through WMI with a local scope
  • The user I am running the script with has admin rights on the machine and can set the default printer manually
  • The user parameters I am creating the remote scope with belong to a domain admin.
  • The script reports success when I run it remotely. No errors are seen.

I am completely stumped as to why the same script with different parameters doesn't work when called using remote WMI. I spent several hours searching and did not find an adequate answer.

Here is the method I am using to create the scope I am connecting to the remote machine with:

public static ManagementScope CreateScope() {
        string nameSpace = @"\\" + Parameters.FQDN + @"\root\cimv2";

        ManagementPath path = new ManagementPath(nameSpace);
        ConnectionOptions Connection = new ConnectionOptions();
        Connection.Username = Parameters.User;  // Username value includes the domain
        Connection.Password = Parameters.Password;
        Connection.Impersonation = ImpersonationLevel.Impersonate;

        return new ManagementScope(path, Connection);

}

Can anyone tell me why the second command line is not setting the printer on the remote machine as the default printer?

1

There are 1 best solutions below

3
Derek On

Hope this Helps. I would advise that you create a batch file at runtime with your two commands and create a process that way. But for now test the final command your having trouble with like this:-

string Command = @"cscript C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -t -p Test002";

ManagemenConnectionOptions connOptions = new ConnectionOptions();
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.EnablePrivileges = true;
tScope manScope = new ManagementScope
    (String.Format(@"\\{0}\ROOT\CIMV2", Parameters.FQDN), connOptions);
manScope.Connect();

ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath("Win32_Process");
ManagementClass processClass = new ManagementClass
    (manScope, managementPath, objectGetOptions);
ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
inParams["CommandLine"] = Command; 

ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);
Console.WriteLine("Creation of the process returned: " + outParams["returnValue"]);
Console.WriteLine("Process ID: " + outParams["processId"]);