Determine Boot Time and Uptime Remotely

by Oct 18, 2017

Get-CimInstance is a useful cmdlet to retrieve WMI information because it uses standard .NET DateTime objects rather than the awkward WMI datetime format. However, Get-CimInstance uses WinRM for remote access whereas the older Get-WmiObject used DCOM for remote access.

Very old systems may not yet be configured to use WinRM remoting, and may still require DCOM. Here is sample code that illustrates how you can use Get-CimInstance with DCOM to query old machines:

# change computer name to a valid remote system that you
# can access remotely
$computername = 'server12'

# use DCOM for older systems that do not run with WinRM remoting
$option = New-CimSessionOption -Protocol Dcom
$session = New-CimSession -ComputerName $computername -SessionOption $option

$bootTime = Get-CimInstance -ClassName Win32_OperatingSystem -CimSession $session | Select-Object -ExpandProperty LastBootupTime
$upTime = New-TimeSpan -Start $bootTime

$min = [int]$upTime.TotalMinutes
"Your system is up for $min minutes now."

Remove-CimSession -CimSession $session

Twitter This Tip! ReTweet this Tip!