PowerShell for Windows Admins

Sep 9 2013   2:04AM GMT

Getting WMI data from remote machines

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: