2

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?

Bruce
  • 21
  • 3
  • Do you get any error messages? I would try doing the same thing using PSTOOLS and the Process Class. It's very strange but could have something to do with group policies. I've had similar issues in the past with WMI when trying to run windows installer files remotely. It was blocking me from doing it if my admin account profile was already on the remote machine. You could try running a batch file on the machine with your commands in it. Good Luck. – Derek Feb 04 '13 at 19:49
  • Thanks for the reply Derek. I have updated my original post with the answer to your question. I don't see any errors when I run the script remotely. – Bruce Feb 04 '13 at 23:39
  • I have checked the domain controller, but was unable to find any group policies that would explain what I am seeing. I tried a new user with admin rights and the script still failed. Right now I am working around this issue with a console application that I have copied to the lab machine. I will try PsTools if no other remote solution presents itself soon, but I really would like to understand why I am seeing this. – Bruce Feb 04 '13 at 23:55

1 Answers1

0

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"]);
Derek
  • 8,300
  • 12
  • 56
  • 88
  • The two differences between your code and mine were the ObjectGetOptions and EnablePrivileges lines. Unfortunately, including those lines did not solve the issue. This is the output when the code runs - `cscript C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -t -p Test002 Out parameters: Process ID: 5600 Creation of the process returned: 0 ` – Bruce Feb 05 '13 at 17:37
  • That means that the Process has been created without error. Its truly weird. Sorry It didn't work. – Derek Feb 05 '13 at 18:32
  • Check this link :- http://stackoverflow.com/questions/6388503/change-default-printer-within-wpf-application – Derek Feb 05 '13 at 18:35