Saturday, October 16, 2010

Powershell to get server uptime

I dont remember from which site i got the below script but it is very useful in my day to day admin life

$wmi=Get-WmiObject -class Win32_OperatingSystem -computer "Computername"
$LBTime=$wmi.ConvertToDateTime($wmi.Lastbootuptime)
[TimeSpan]$uptime=New-TimeSpan $LBTime $(get-date)
Write-host $server “Uptime: ” $uptime.days “Days” $uptime.hours “Hours” $uptime.minutes “Minutes” $uptime.seconds “Seconds”

Get-WmiObject -class Win32_OperatingSystem
This will invoke the Operating System wmi object


ConvertToDateTime
Will convert the output to human readable form


[TimeSpan]$uptime=New-TimeSpan $LBTime $(get-date)
This will compare the output time with the current time and store the result to the string as object

No comments: