VBScript 支持对象内省吗?

发布于 2024-12-08 06:27:44 字数 680 浏览 0 评论 0原文

我正在通过 VBScript 使用 WQL 从 WMI 拉回结果。

在示例中,For Each 循环用于迭代结果,但在每个示例中,假设属性名称已知。恰当的例子:

Set colInstalledPrinters = objWMIService.ExecQuery ("Select * from Win32_Printer Where Default = True")
For Each objPrinter in colInstalledPrinters
    Wscript.Echo objPrinter.Name
Next

某些 WMI 类具有很长的与其关联的属性列表。另一个复杂因素是,某些属性不能期望出现(根据我读过的有关 WMI 的各种网页)。我不想研究每个 WMI 类并希望列出的属性都存在,而是希望获取 objPrinter 或任何其他返回项的属性(或列,如果我在 SQL/WQL 中思考的话)存在的列表。

Python 是我常用的语言,但在本例中我无法将其安装在目标计算机上;我可以通过 Python 执行 WMI 远程查询,但我试图触发本地事件,因此又回到了 VBScript。虽然我认为 Powershell 可能能够做到这一点,但我不想现在就学习它。

那么,VBScript 是否支持允许我枚举属性列表的内省级别?或者我可以做一些涉及我可以在脚本中引用和检查的模式的事情吗?

I am pulling results back from WMI using WQL via VBScript.

In examples, a For Each loop is used to iterate over the results, but in each example, it is assumed that the property names are known. Case in point:

Set colInstalledPrinters = objWMIService.ExecQuery ("Select * from Win32_Printer Where Default = True")
For Each objPrinter in colInstalledPrinters
    Wscript.Echo objPrinter.Name
Next

Some of the WMI classes have a very long list of properties associated with them. As an additional complication, some properties cannot be expected to be present (according to various webpages I have read about WMI). Rather than researching each WMI class and hoping that the properties listed are present, I would like to obtain a list of the properties (or columns, if I am thinking in SQL/WQL) present for, say, an objPrinter or any other returned item.

Python is my usual language but I cannot install it on the target machines in this instance; I can perform remote querying of WMI via Python but I am trying to trigger on an local event, hence falling back to VBScript. Although I gather Powershell might be able to do this, I would rather not learn it just this instant.

So, does VBScript support that level of introspection which would allow me to enumerate a list of properties? Or is there something I can do involving a schema I can reference and examine in-script?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

情绪操控生活 2024-12-15 06:27:44

使用该项目的 .Properties_ 集合:

Option Explicit

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Dim objWMIService
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Dim colItems
Set colItems = objWMIService.ExecQuery( _
     "SELECT * FROM Win32_Printer" _
   , "WQL" _
   , wbemFlagReturnImmediately + wbemFlagForwardOnly _
)
Dim objItem
For Each objItem In colItems
    Dim oProp
    For Each oProp In objItem.Properties_
        WScript.Echo oProp.Name, TypeName( oProp.Value ), ToString( oProp.Value )
    Next
    WScript.Echo
Next

Function ToString( vX )
  ToString = "!! work to do !!"
 On Error Resume Next
  ToString = CStr( vX )
 On Error GoTo 0
End Function

输出:

...
MimeTypesSupported Null !! work to do !!
Name String Auto HP LaserJet 5 on WINXP2
NaturalLanguagesSupported Null !! work to do !!
Network Boolean False
PaperSizesSupported Variant() !! work to do !!
...

显然,ToString() 函数需要进一步工作。

Use the .Properties_ collection of the item:

Option Explicit

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Dim objWMIService
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Dim colItems
Set colItems = objWMIService.ExecQuery( _
     "SELECT * FROM Win32_Printer" _
   , "WQL" _
   , wbemFlagReturnImmediately + wbemFlagForwardOnly _
)
Dim objItem
For Each objItem In colItems
    Dim oProp
    For Each oProp In objItem.Properties_
        WScript.Echo oProp.Name, TypeName( oProp.Value ), ToString( oProp.Value )
    Next
    WScript.Echo
Next

Function ToString( vX )
  ToString = "!! work to do !!"
 On Error Resume Next
  ToString = CStr( vX )
 On Error GoTo 0
End Function

Output:

...
MimeTypesSupported Null !! work to do !!
Name String Auto HP LaserJet 5 on WINXP2
NaturalLanguagesSupported Null !! work to do !!
Network Boolean False
PaperSizesSupported Variant() !! work to do !!
...

Obviously, the ToString() function needs further work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文