Categories

Getting WMI data from remote machines

WMI is great for pulling back data from remote machines. This type of activity is quite common:

$computer = $env:COMPUTERNAME

$comp = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computer
$os = Get-WmiObject -Class Win32_OperatingSystem  -ComputerName $computer
$disk = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $computer

New-Object -TypeName PSObject -Property @{
Name = $computer
OS = $os.Caption
LastReBoot = $os.LastBootUpTime
Type =  "$($comp.Manufacturer) : $($comp.Model)"
AvailableRAM = $comp.TotalPhysicalMemory
Disks = ($disk | Measure).Count
}

Define the computer. Run some WMI queries & pull the data together into a single object for output.

Each time you call Get-WmiObject you have to rebuild the connection to the remote machine. There appears to be a little bit of caching of information so its not a complete rebuild of the connection but the system has to do some work.

For a few calls to WMI its not a big deal but what you need to make more calls – say 8 or 10.

The CIM cmdlets introduced the concept of CIM sessions for this scenario. Bu the CIM cmdlets work over WSMAN by default & if you have machines running legacy versions of PowerShell or even no PowerShell you can’t use CIM sessions.

Oh yes you can!  (sorry but pantomime season is rapidly approaching)

You just configure you CIM session to work over DCOM – just like WMI cmdlets.

$computer = $env:COMPUTERNAME
$copt = New-CimSessionOption -Protocol Dcom
$csess = New-CimSession -SessionOption $copt -ComputerName $computer

$comp = Get-CIMInstance -ClassName Win32_ComputerSystem -CimSession $csess
$os = Get-CIMInstance -ClassName Win32_OperatingSystem  -CimSession $csess
$disk = Get-CIMInstance -ClassName Win32_LogicalDisk -CimSession $csess

$csess | Remove-CimSession

New-Object -TypeName PSObject -Property @{
Name = $computer
OS = $os.Caption
LastReBoot = $os.LastBootUpTime
Type =  "$($comp.Manufacturer) : $($comp.Model)"
AvailableRAM = $comp.TotalPhysicalMemory
Disks = ($disk | Measure).Count
}

The session option is defined with the DCOM protocol

The Get-CimInstance calls use –CimSession instead of –ComputerName

Best of all the LastBootUpTime is converted to a proper date

If you’re thinking of using multiple WMI calls to a remote machine – think about a CIM session

2 Responses to Getting WMI data from remote machines

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>