Server Uptime Script

Server Uptime Script

1. create txt file "Servers.txt"

2.  insert server hostname or IP

3. Run script

Powershell code -

 

$names = Get-Content "C:\Uptime\Servers.txt"

        @(

   foreach ($name in $names)

      {

            if ( Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue ) 

  {

            $wmi = gwmi -class Win32_OperatingSystem -computer $name

    $LBTime = $wmi.ConvertToDateTime($wmi.Lastbootuptime)

    [TimeSpan]$uptime = New-TimeSpan $LBTime $(get-date)

    Write-output "$name Uptime is  $($uptime.days) Days $($uptime.hours) Hours $($uptime.minutes) Minutes $($uptime.seconds) Seconds"

  }

             else {

                Write-output "$name is not pinging"

          }

        }

) | Out-file -FilePath "C:\Uptime\UpUptime_results.txt"

No comments: