Being a bit of a newbie, I'm having trouble getting the script below to
work. I'm trying to create a piece of code that will allow me to supply
3 parameters (a computer name, a WMI class and a WMI class property)
and get a value returned. For ease of reading I've hard-coded the
variable in the example below to make things more obvious.


strComputerName = "Myserver"
strClass = "Win32_Processor"
strProperty = "MaxClockSpeed"

Set objWMIService = GetObject("winmgmts:\\" & strComputerName &
"\root\cimv2")
Set objClass = objWMIService.Get(strClass)

For Each objProperty in objClass.properties_
If objProperty.Name = strProperty Then
strValue = objProperty.Value
WScript.Echo objProperty.Name & " = " & strValue
End If
Next


Any ideas why strValue never gets any value?

Re: Help with vbs / wmi script by Torgeir

Torgeir
Fri Jul 22 11:34:45 CDT 2005

gjscott75 wrote:

> Being a bit of a newbie, I'm having trouble getting the script below to
> work. I'm trying to create a piece of code that will allow me to supply
> 3 parameters (a computer name, a WMI class and a WMI class property)
> and get a value returned. For ease of reading I've hard-coded the
> variable in the example below to make things more obvious.
>
>
> strComputerName = "Myserver"
> strClass = "Win32_Processor"
> strProperty = "MaxClockSpeed"
>
> Set objWMIService = GetObject("winmgmts:\\" & strComputerName &
> "\root\cimv2")
> Set objClass = objWMIService.Get(strClass)
>
> For Each objProperty in objClass.properties_
> If objProperty.Name = strProperty Then
> strValue = objProperty.Value
> WScript.Echo objProperty.Name & " = " & strValue
> End If
> Next
>
>
> Any ideas why strValue never gets any value?
>
Hi,

An alternative way of doing it:

'--------------------8<----------------------

strComputerName = "Myserver"
strClass = "Win32_Processor"
strProperty = "MaxClockSpeed"

Set objWMIService = GetObject("winmgmts:\\" & strComputerName & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
("Select " & strProperty & " from " & strClass)

On Error Resume next
intCount = colItems.Count
On Error Goto 0

If intCount > 0 Then
For Each objItem in colItems
strValue = Eval("objItem." & strProperty)
WScript.Echo strValue
Next
Else
WScript.Echo "Property or class does not exist"
End If
'--------------------8<----------------------


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx

Re: Help with vbs / wmi script by gjscott75

gjscott75
Sat Jul 23 07:01:02 CDT 2005

That works perfectly, many thanks for your help.

Graeme