为什么 WMI 对同一台计算机返回不同的结果?
我正在对一些旨在查询远程计算机的默认打印机的 vbscript 进行故障排除时,发现脚本中的错误实际上是因为 WMI 返回冲突甚至错误的信息。请看这个截图:
两者都针对同一台计算机 (CLIFGRIFFIN-PC)。左侧是从 Windows Server 2003 安装运行的。正确的一个是在 PC 上运行的。
左边的打印机除了结果较少之外,还显示没有打印机是默认的。这就是导致脚本错误的原因,促使我对此进行调查。
有什么想法吗?这太奇怪了。
编辑: 这是我正在使用的代码...
Set WshShell = CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections
Dim strComputer
strComputer = WshShell.ExpandEnvironmentStrings("%CLIENTNAME%")
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery ("Select * from Win32_Printer Where Default = True")
For Each Printer in colInstalledPrinters
For i = 0 to OPrinters.Count - 1 Step 2
If InStr(objPrinters.Item(i+1), Printer.Name) > 0 Then
WshNetwork.SetDefaultPrinter(objPrinters.Item(i+1))
Exit For
End If
Next
Next
I was troubleshooting some vbscript designed to query the default printer of a remote computer when I discovered the error in the script was actually because WMI is returning conflicting or even erroneous information. Please see this screenshot:
Both are against the same computer (CLIFGRIFFIN-PC). The left one is run from a Windows Server 2003 install. The right one is run on the PC itself.
The left one, in addition to having fewer results, also shows that NONE of the printers are default. Which is what is causing the scripting error that led me to investigate this.
Any ideas? It's just bizarre.
EDIT:
Here is the code I'm using...
Set WshShell = CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections
Dim strComputer
strComputer = WshShell.ExpandEnvironmentStrings("%CLIENTNAME%")
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery ("Select * from Win32_Printer Where Default = True")
For Each Printer in colInstalledPrinters
For i = 0 to OPrinters.Count - 1 Step 2
If InStr(objPrinters.Item(i+1), Printer.Name) > 0 Then
WshNetwork.SetDefaultPrinter(objPrinters.Item(i+1))
Exit For
End If
Next
Next
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如所说,这可能取决于登录的用户。
当您从远程电脑查询时,您将只能获得本地安装的打印机。
当您从同一台电脑查询时,您还将获得所有网络连接的打印机。
这里的网络连接意味着它们在其他电脑上共享。
当您添加网络打印机时会有点混乱,但它在 Windows 中称为本地打印机,但它有一个 ip 端口。
It may depend on the loged in user as were said.
when you query from a remote pc you will only get locally installed printers as a result.
when you query from the same pc you will get all network connected printers as well.
Network connected here means they are shared on other pcs.
There is a bit of confusion when you add a network printer but it is called local in windows but it has a ip port.
尝试将模拟级别设置为模拟 ie
Try setting the impersonation level to impersonate i.e.