Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Powershell & "WbemScripting.SWbemRefresher"

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 08-01-2007   #1 (permalink)
dallen16
Guest


 

Powershell & "WbemScripting.SWbemRefresher"

I'm about day 5 into using Powershell... and I'm trying to figure out
how to use 'SWbemRefresher' to monitor local network traffic from within
a Powershell script.

The classic VBscript solution starts as follows...

******

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set objRefresher = CreateObject("WbemScripting.SWbemRefresher")
Set colItems = objRefresher.AddEnum _
(objWMIService,
"Win32_PerfFormattedData_TCPIP_NetworkInterface").objectSet


******

Translated to Powershell should look something like the following... but
the first line is "invalid" because -class wants one of the 900+ WMI
class names.

******

$objWMIService = get-wmiobject -class "winmgmts" -namespace "root\cimv2"
-computername '.'
$objRefresher = new-object -com "WbemScripting.SWbemRefresher"
$colItems = $objRefresher.AddEnum($objWMIService,
"Win32_PerfFormattedData_TCPIP_NetworkInterface").objectSet

******

I can't figure out which wmiobject class to specify... and how to
specify impersonation level...

I've tried a few of the more obvious looking class names
("Win32_Service", "CIM_Service", "Win32_Perf", ...) but these either
hang on the get-wmiobject call or generate an exception with the AddEnum
call like the following.

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


Or is there another way altogether to accomplish the same thing?

Any help greatly appreciated!!!

Regards,

.... Dewey

My System SpecsSystem Spec
Old 08-02-2007   #2 (permalink)
Marco Shaw
Guest


 

Re: Powershell & "WbemScripting.SWbemRefresher"

dallen16 wrote:
> I'm about day 5 into using Powershell... and I'm trying to figure out
> how to use 'SWbemRefresher' to monitor local network traffic from within
> a Powershell script.
>
> The classic VBscript solution starts as follows...
>
> ******
>
> strComputer = "."
> Set objWMIService = GetObject("winmgmts:" _
> & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
> set objRefresher = CreateObject("WbemScripting.SWbemRefresher")
> Set colItems = objRefresher.AddEnum _
> (objWMIService,
> "Win32_PerfFormattedData_TCPIP_NetworkInterface").objectSet
>
>
> ******
>
> Translated to Powershell should look something like the following... but
> the first line is "invalid" because -class wants one of the 900+ WMI
> class names.
>
> ******
>
> $objWMIService = get-wmiobject -class "winmgmts" -namespace "root\cimv2"
> -computername '.'
> $objRefresher = new-object -com "WbemScripting.SWbemRefresher"
> $colItems = $objRefresher.AddEnum($objWMIService,
> "Win32_PerfFormattedData_TCPIP_NetworkInterface").objectSet
>
> ******
>
> I can't figure out which wmiobject class to specify... and how to
> specify impersonation level...
>
> I've tried a few of the more obvious looking class names
> ("Win32_Service", "CIM_Service", "Win32_Perf", ...) but these either
> hang on the get-wmiobject call or generate an exception with the AddEnum
> call like the following.
>
> Exception calling "AddEnum" with "2" argument(s): "Type mismatch.
> (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"
>
>
> Or is there another way altogether to accomplish the same thing?
>
> Any help greatly appreciated!!!
>
> Regards,
>
> ... Dewey


I'm a bit ignorant here...

Why can't you simply use something like:

PS> $class="win32_perfrawdata_tcpip_networkinterface"
PS> $namespace="root\cimv2"
PS> get-wmiobject -class $class -namespace $namespace
My System SpecsSystem Spec
Old 08-05-2007   #3 (permalink)
urkec
Guest


 

Re: Powershell & "WbemScripting.SWbemRefresher"

"Marco Shaw" wrote:

> dallen16 wrote:
> > I'm about day 5 into using Powershell... and I'm trying to figure out
> > how to use 'SWbemRefresher' to monitor local network traffic from within
> > a Powershell script.


Same situation here...

> > Regards,
> >
> > ... Dewey

>
> I'm a bit ignorant here...
>
> Why can't you simply use something like:
>
> PS> $class="win32_perfrawdata_tcpip_networkinterface"
> PS> $namespace="root\cimv2"
> PS> get-wmiobject -class $class -namespace $namespace
>


Problem is, when you do that, WMI object information don't get refreshed.
For example,if I use this:

$proc = get-wmiobject -class "Win32_Process"

I can get number of runninng processes with this:

$proc.Count

But if I create or kill some processes and do this again:

$proc.Count

the number of processes is the same as the first time, and

$proc.IsSynchronized

is false. In VBScript I could add WMI objects to a SWbemRefresher object
and use SWbemRefresher.Refresh to refresh data for all objects added to it.
Using PowerShell, I managed to do something like this:

$objWMILocator = new-object -com "WbemScripting.SWbemLocator"
$objWMIService = $objWMILocator.ConnectServer(".","root\cimv2")
$objRefresher = new-object -com "WbemScripting.SWbemRefresher"
$objRefresher.AddEnum($objWMIService,"Win32_Process").objectSet

but that is just rewriting VBScript code in PowerShell. I have been looking
into .Net System.Management namespace but I haven't been able to find
anything that looks like SWbemRefresher. I know I could use get-wmiobject
multiple times to update object data, but SWbemRefresher was added to WMI
scripting library just to avoid that kind of thing. I am new to PowerShell
and .Net and I can't figure how to do this one.
Hopefully someone can help.

--
urkec
My System SpecsSystem Spec
Closed Thread
Update your Vista Drivers Update Your Drivers Now!!

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unwanted Multiple contacts in "To","CC","BCC" of email send catago xsailer Vista mail 1 07-26-2008 08:34 AM
Vista not wotking with "My Computer" or "Control Panel", "Screen Saver" Platebanger Vista General 6 02-05-2008 08:54 AM
How can I add the icons "Delete", "Cut", "Copy" and "Paste" in Vis Moonwalker Vista file management 7 09-17-2007 05:55 PM
WM5 Sync with Vista "Windows Calender", "Contacts", and "Mail" Tony Vista General 1 02-16-2007 06:20 PM
Windows PowerShell and the "PowerShell Worm" Jeffrey Snover [MSFT] PowerShell 5 08-05-2006 03:56 AM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51