PowerShell Live - being retired

Please go to http://www.shelltools.net for more information

Welcome to PowerShell Live - being retired Sign in | Join | Help
Home Main Site Blogs Forums Videos Chat Customer Support

Powershell and WbemScripting.SWbemRefresher or How to Refresh WMI Data in Powershell

  •  08-01-2007, 9:11 PM

    Powershell and WbemScripting.SWbemRefresher or How to Refresh WMI Data in Powershell

    I'm about day 5 into using Powershell...(and 25 years of writing code) and I'm trying to figure out how to use 'SWbemRefresher' to monitor local network traffic from within a Powershell script (which is scripting IE to click through a web application.)
    
    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
    
    ******
    See the following URLs...
    http://msdn2.microsoft.com/en-us/library/aa393026.aspx
     http://www.activexperts.com/activmonitor/windowsmanagement/scripts/networking/monitoring/
    http://www.microsoft.com/technet/scriptcenter/resources/qanda/apr05/hey0421.mspx
     
    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...  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))"
    And  I saw another cmdlet that looked promising "get-service" ... for which there is a "winmgmt" service...but no "winmgmts" service... but this doesn't seem to work either.
    
    Or is there another way altogether to accomplish the same thing?
    
    Any help greatly appreciated!!!
    
    Regards,
    
    .... Dewey
    
    Filed under:
View Complete Thread