为什么 WMI 对同一台计算机返回不同的结果?

发布于 2024-09-30 21:15:14 字数 1113 浏览 2 评论 0原文

我正在对一些旨在查询远程计算机的默认打印机的 vbscript 进行故障排除时,发现脚本中的错误实际上是因为 WMI 返回冲突甚至错误的信息。请看这个截图: alt text

两者都针对同一台计算机 (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:
alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过潦 2024-10-07 21:15:14

正如所说,这可能取决于登录的用户。

当您从远程电脑查询时,您将只能获得本地安装的打印机。
当您从同一台电脑查询时,您还将获得所有网络连接的打印机。
这里的网络连接意味着它们在其他电脑上共享。

当您添加网络打印机时会有点混乱,但它在 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.

浅唱々樱花落 2024-10-07 21:15:14

尝试将模拟级别设置为模拟 ie

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Try setting the impersonation level to impersonate i.e.

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文