Daniyal
Daniyal

Reputation: 17

Unable to use the value of a parameter to query a WMI instance

this is a very basic program I wanted to write a VB script which I'll call from command line and pass in a few parameters, and the VB script should take the parameters and query some information from WMI, the code is like this:

1.strProperty = colNamedArguments.Item("Property") //this is to store the parameter
2.str..... some other parameters
3.Set objSWbemServices = GetObject("winmgmts:\\" & strComputer & strNamespace)
4.Set colSWbemObjectSet = objSWbemServices.ExecQuery("SELECT * FROM " & strClass)
5.For Each objSWbemObject In colSWbemObjectSet
6.  wscript.echo objSWbemObject.strProperty

1 and 2. is to store the parameter 3.is to connect to the WMI 4.is to query the data needed 5 and 6.is to run through the collected data and print them out. The problem I have is that when I run the script with the correct parameters it prints out nothing, but if I replace the strProperty in line 5 with actual parameter I passed with command it works, and to test if the parameter is passing properly I have add lines of codes to print it out soon as it gets the parameter that worked. I think the line 5 is using the "strProperty" as the property to match the data but not the string value of the strProperty.

Upvotes: 0

Views: 400

Answers (1)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

If you really want what I gather from your description (access a property specified at runtime), this

  Dim sComputer : sComputer = "."
  Dim sWQL      : sWQL      = "Select * from Win32_LogicalDisk"
  Dim sProperty : sProperty = "Name" ' "DriveType"
  Dim oWMIS     : Set oWMIS = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
  Dim oColl, oItem
  Set oColl = oWMIS.ExecQuery(sWQL)
  For Each oItem in oColl
      WScript.Echo sProperty & ":", oItem.Properties_(sProperty).Value
  Next

output:

Name: A:
Name: C:
Name: D:
Name: E:
Name: M:

or:

DriveType: 2
DriveType: 3
DriveType: 5
DriveType: 4
DriveType: 4

should get you started: use the Properties_ 'dictionary' of the item.

Upvotes: 1

Related Questions