Monday, December 30, 2013

Uptime in Powershell using WMI

The below script is written in power shell and is used to compute up-time for remote Windows system using WMI WIn32_OperatingSystem . You need to have Power shell installed to run this and also would require Administrative rights on Remote hosts  which you are targeting this script. Copy the below script in green to notepad and save it as get-uptime.ps1.

Go to powershell, and set-location to the directory where you have saved the script.

Execute the script .\get-uptime.ps1 hostname to get the uptime of remote system

#Script to Find the uptime of remote host
#pass the paramter hostname after the script during usage
#Author: Shashanka Haritsa 2011
#Function to Convert Date String
Function datestringtodate($stringdata){
if ($args -eq “Help”) {
Write-host “Usage get-uptime.ps1 hostname” -Foregroundcolor Cyan
Write-host “Ex: get-uptime.ps1 server01, where server01 is the hostname” -Foregroundcolor Cyan
$lastuptimedate = (get-wmiobject win32_operatingsystem -Computername $args).LastBootUpTime
$convert = datestringtodate($lastuptimedate)
$currentdate =(get-wmiobject win32_operatingsystem -Computername $args).localDateTime
$convert1 =datestringtodate($currentdate)
$up =$convert1-$convert
$uptimet = ” System is up for ” + $up.Days + ” Days ” + $up.Hours + ” Hours ” + $up.Minutes + ” Minutes ” + $up.Milliseconds + ” Milliseconds”
#End of the script
I hope it helps you all!

No comments:

Post a Comment