One of the most common questions asked when working with projects either for patching or testing or any other reason is when servers last rebooted. I have found the WMI class which will give us what we need.

Win32_OperatingSystem

So what can I get out of this class?

Get-WMIObject Win32_OperatingSystem | Get-Member

Get-CimInstance Win32_OperatingSystem | Get-Member

There is a difference we will see. Look at the definitions. This will reflect to their output.

LASTWMI

LastCIM

and LastBootUpTime what we need and we need to pull this info out of here;

Get-WMIObject -ClassName win32_OperatingSystem | select csname, lastbootuptime

csname                 lastbootuptime
——                  ————–
USER-PC             20160707090526.982130+060

And

Get-CimInstance -ClassName win32_OperatingSystem | select csname, lastbootuptime

csname                  lastbootuptime
——                   ————–
USER-PC               7/7/2016 9:05:26 AM

So looks like we need to convert the time if you use Get-WMIObject

>$wmi = gwmi win32_operatingsystem
>$wmi.ConvertToDateTime($wmi.LastBootUpTime)

or

$LastBootUpTime = Gwmi Win32_OperatingSystem -Comp server01 | Select -Exp LastBootUpTime
[System.Management.ManagementDateTimeConverter]::ToDateTime($LastBootUpTime)
or

>gwmi win32_operatingsystem | %{ $_.ConvertToDateTime($_.LastBootUpTime) }

or

>$BootTime = Invoke-Command -Cn server01, server02`
  -Command { (gwmi win32_operatingsystem).lastbootuptime }
>$BootTime | foreach { ([wmi]'').ConvertToDateTime($_) }

Why do you need to go through these conversions, just use Get-CimInstance...

>$BootTimes = Get-CimInstance -Cn server1, server2-Class Win32_OperatingSystem |
    Select PSComputerName, LastBootUpTime

>$BootTimes | Format-Table -AutoSize

PSComputerName LastBootUpTime       
-------------- --------------       
server1        8/18/2016 15:40:32 PM 
server2        8/19/2016 20:50:53 PM
 If you have got a few servers put them in a txt file  (serverlist.txt) and use;

$compname = Get-Content -Path C:\serverlist.txt
foreach ($comp in $compname) {
    Get-WmiObject win32_operatingsystem -ComputerName $comp | '
select CSName, @{LABEL='LastBootUpTime';'
EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}

}
 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s