我正在使用WMI作为域管理员连接到我的实验室计算机。然后我运行此命令行来创建一个打印机:
cscript C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -a -p Test002 -m "Canon Inkjet iP100 series" -r FAKE002
工作正常。
然后我运行此命令行将打印机设置为默认值:
cscript C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -t -p Test002
这根本不起作用。
一些相关细节:
我完全难以理解为什么使用远程WMI调用时,具有不同参数的相同脚本不起作用。我花了几个小时搜索,但没有找到合适的答案。
这是我用来创建我连接到远程机器的范围的方法:
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);
}
有谁能告诉我为什么第二个命令行没有将远程计算机上的打印机设置为默认打印机?
答案 0 :(得分:0)
希望这有助于。我建议您在运行时使用两个命令创建一个批处理文件,并以此方式创建一个进程。但是现在用这样测试最终命令你有问题: -
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"]);