PowerShell Script using WMI class to check uptime of the remote machines – Suptime.ps1

Hello.. Everyone,

Good Day !!!!

I have written one more script to check uptime of the server remote machines that uses PowerShell and WMI class “Win32_OperatingSystem” to check the uptime of the server

The script first checks if the server/computer is reachable then it tries to get uptime of that machine, if the computer/server is not reachable then the script throws a output stating “The server is not reachable”


Content of sutime.ps1
——————————————————————————–
# PS script to get uptime of the computers mentioned in computers.txt file
# Script first checks if the server is reachable then tries to get uptime of the server
# Function to get uptime

Function Get-HostUptime {
    param ([string]$ComputerName)
    $Uptime = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName
    $LastBootUpTime = $Uptime.ConvertToDateTime($Uptime.LastBootUpTime)
    $Time = (Get-Date) – $LastBootUpTime
    Return ‘{0:00} Days, {1:00} Hours, {2:00} Minutes, {3:00} Seconds’ -f $Time.Days, $Time.Hours, $Time.Minutes, $Time.Seconds
}

foreach ($computer in Get-Content “computers.txt”)
{
$c = Get-WmiObject Win32_PingStatus -f “Address=’$computer'”

if($c.StatusCode -eq 0) 
{

$sysuptime = Get-HostUptime -ComputerName $computer

write-host “$computer” “$sysuptime”
}

else
{

write-host “$computer is not reachable”

}
}
——————————————————————————–

Advertisements

About asifkhandevadi

Hello, I have been working on windows since 9 years and currently working as windows, VMware and MS clustering SME at IBM. Whenever I get free time I participate in Microsoft forums and write some blogs to enhance my technical and communication skills through knowledge sharing. Please contact me on FB or Linkedin if you need any assistance on troubleshooting, implementation and virtualizaton.
This entry was posted in Power-Shell. Bookmark the permalink.

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