Home |

Trigat

Uptime and Ping Status with PowerShell

10-11-2017

Language or Platform: Powershell

Code:

################################################################################
# This PS script provides Uptime and Pingstatus for list of computers. 
################################################################################
# Create a C:\serverlist directory and place servers.txt and results.txt inside
################################################################################
        $names = Get-Content "servers.txt" 
        @( 
           foreach ($name in $names) 
          { 
            if ( Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue )  
          { 
            $wmi = gwmi 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 "results.txt"

Back