Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to use swbemrefresher object to refresh wmi performance data

266 views
Skip to first unread message

John den Braber

unread,
Dec 11, 2007, 3:03:44 PM12/11/07
to
Hi guys,

I am currently investigating the use of powershell in gathering
performance data from my servers through the use of wmi performance
classes (i.e. Win32_PerfFormattedData_PerfOS_Processor //
http://msdn2.microsoft.com/en-us/library/aa394271.aspx. )

Normally a refresher object is used. It contains instances of MOF
classes so that the data can be updated. Performance classes that derive
from Win32_Perf require the use of a refresher object to get performance
data but I can not figure out how to accomplish this in Powershell.

Getting the wmi performance data itself is really easy in powershell,
but it is just the refresher functionality which allows to reread the
performance values without reconnecting to wmi that I would like to
achieve.

Any help will be really appreciated!!

Cheers!
John den Braber

In vbs it would look like this (ms example):

------------


On Error Resume Next
strComputer = "."
Set objRefresher = CreateObject("WbemScripting.SWbemRefresher")
Set objServicesCimv2 = GetObject("winmgmts:\\" _
& strComputer & "\root\cimv2")
If Err = 0 Then
Set objRefreshableItem = _
objRefresher.AddEnum(objServicesCimv2 , _
"Win32_PerfFormattedData_PerfProc_Process")
objRefresher.Refresh
' Loop through the processes three times to locate
' and display all the process currently using
' more than 1 % of the process time. Refresh on each pass.
For i = 1 to 3
Wscript.Echo "Refresh number " & i
objRefresher.Refresh
For Each Process in objRefreshableItem.ObjectSet
If Process.PercentProcessorTime > 1 then
WScript.Echo Process.Name & _
vbnewLine & Process.PercentProcessorTime & "%"
End If
Next
Next
Else
WScript.Echo Err.Description
End If


-----------

Marco Shaw [MVP]

unread,
Dec 11, 2007, 8:28:27 PM12/11/07
to
John den Braber wrote:
> Hi guys,
>
> I am currently investigating the use of powershell in gathering
> performance data from my servers through the use of wmi performance
> classes (i.e. Win32_PerfFormattedData_PerfOS_Processor //
> http://msdn2.microsoft.com/en-us/library/aa394271.aspx. )
>
> Normally a refresher object is used. It contains instances of MOF
> classes so that the data can be updated. Performance classes that derive
> from Win32_Perf require the use of a refresher object to get performance
> data but I can not figure out how to accomplish this in Powershell.
>
> Getting the wmi performance data itself is really easy in powershell,
> but it is just the refresher functionality which allows to reread the
> performance values without reconnecting to wmi that I would like to
> achieve.

Check this thread out:
http://www.scriptinganswers.com/forum2/forum_posts.asp?TID=985&KW=swbemrefresher

I seem to remember another.

If that doesn't work for you let us know.

Marco


--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com

John den Braber

unread,
Dec 12, 2007, 3:54:13 AM12/12/07
to
Marco Shaw [MVP] wrote:
> John den Braber wrote:
>> Hi guys,
>>
>> I am currently investigating the use of powershell in gathering
>> performance data from my servers through the use of wmi performance
>> classes (i.e. Win32_PerfFormattedData_PerfOS_Processor //
>> http://msdn2.microsoft.com/en-us/library/aa394271.aspx. )
>>
>> Normally a refresher object is used. It contains instances of MOF
>> classes so that the data can be updated. Performance classes that
>> derive from Win32_Perf require the use of a refresher object to get
>> performance data but I can not figure out how to accomplish this in
>> Powershell.
>>
>> Getting the wmi performance data itself is really easy in powershell,
>> but it is just the refresher functionality which allows to reread the
>> performance values without reconnecting to wmi that I would like to
>> achieve.
>
> Check this thread out:
> http://www.scriptinganswers.com/forum2/forum_posts.asp?TID=985&KW=swbemrefresher
>
>
> I seem to remember another.
>
> If that doesn't work for you let us know.
>
> Marco
>
>


Thanks Marco,

I had already googled that one as well but this example does not fly.

The basic problem I assume is in it that the refresher addenum method is
expecting the following:

objRefreshEnum = AddEnum(
objWbemService,
strClass,
[ iFlags = 0 ],
[ objWbemNamedvalueSet = null ]
)

The objWbemservice is supposed to represent a connection to a namespace
in which the objext that is being used in "strClass" resides in.

the strclass is a string that contains the class wich is passed to the
refresher.

However the following errs with a type mismatch which makes me belive
that the $wmiobject is of an incorrect type (should I reconstruct a com
object that represents this wmi namespace connection??)

The example below errs with the type mismatch:

$objWMIService = get-wmiobject -namespace "root/default" -class
__Win32Provider

$objRefresher = new-object -com "WbemScripting.SWbemRefresher"

$colItems =
$objRefresher.AddEnum($objRefresher,"Win32_PerfFormattedData_TCPIP_NetworkInterface")


Exception calling "AddEnum" with "2" argument(s): "Type mismatch.
Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"


Any help appreciated,

Cheers,
John


Marco Shaw [MVP]

unread,
Dec 12, 2007, 7:59:02 AM12/12/07
to
> $objRefresher = new-object -com "WbemScripting.SWbemRefresher"

Well, first off, what gets me here is that if I try:

$objRefresher|get-member

I get *nothing*. This leads me to believe that this COM object just
won't work with PowerShell.

(It doesn't appear to be a threading issue either which is possible with
COM:
http://blogs.msdn.com/powershell/archive/2007/03/23/thread-apartmentstate-and-powershell-execution-thread.aspx)

Marco

John den Braber

unread,
Dec 12, 2007, 2:53:22 PM12/12/07
to

Yep.. Perhaps somebody knows a different way of achieving this through
Powershell gwmi? I can't imagine that this is't possible.. This is so
useful in writing good performance monitoring scripts.

John

urkec

unread,
Dec 13, 2007, 2:12:00 PM12/13/07
to
"John den Braber" wrote:

>
> Yep.. Perhaps somebody knows a different way of achieving this through
> Powershell gwmi? I can't imagine that this is't possible.. This is so
> useful in writing good performance monitoring scripts.
>
> John
>
>
>
>

When I started experimenting with WMI in Powershell, one of the first things
I noticed was the lack of the Refresher object. I was able to use the WMI
scripting library like this:

$Locator = new-object -comobject "WbemScripting.SWbemLocator"
$WMI = $Locator.ConnectServer(".", "root\cimv2")
$Refresher = new-object -comobject "WbemScripting.SWbemRefresher"
$Enum = $Refresher.AddEnum($WMI, "Win32_Process")
$Objset = $Enum.ObjectSet
$Refresher.Refresh()
$Objset.Item("Win32_Process.Handle='0'").Properties_.Item("KernelModeTime")
$Refresher.Refresh()
$Objset.Item("Win32_Process.Handle='0'").Properties_.Item("KernelModeTime")

This seems to work. Unfortunatelly, when I try to use one of the performance
classes, like this:

$Enum = $Refresher.AddEnum($WMI, "Win32_PerfRawData_PerfProc_Thread")

I always get 'Access denied' error, so I'm stuck. Anyway this all looks
like rewriting VBScript code in Powershell, so I went on looking for
something that can be used for refreshing objects in System.Management
namespace. The closest thing I could find was
ManagementObjectSearcher.Get(), but this is not exactly the same as the
Refresher object, it looks more like SWbemServices.ExecQuery, which I needed
to use to refresh object data before Windows XP. I am really surprised not
being able to find anything that looks like SWbemRefesher in
System.Management. If you find a solution to this, please post it here.


--
urkec

John den Braber

unread,
Dec 15, 2007, 2:39:41 PM12/15/07
to

Thanks Urkec,

You have placed indeed a working rewrite like in vbs or perl, and I too
get the access denied when used with a performance class:

Exception calling "AddEnum" with "2" argument(s): "Access denied "
At line:1 char:28
+ $Enum = $Refresher.AddEnum <<<< ($WMI,
"Win32_PerfFormattedData_PerfOS_Processor")

I'll have another play around with it this way.

The refresher object is such a trivial piece in retrieving performance
data from your systems that it is hard to believe there is no
implementation of the refresher functionality in powershell.

Too bad nobody from the Powershell development team here to answer/explain..

Cheers!
John

0 new messages