Powershell to check uptime of servers

I am newbie for powershell. I got a script to check server uptime but I don't know

1) how to output the result?

2) how to change the path of computer.txt==foreach ($computer in Get-Content "computers.txt")?

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"

}
}

May 7th, 2014 6:39pm

The output will be in the PowerShell console, via the Write-Host cmdlet's

To change the computers.txt simply modify

foreach ($computer in Get-Content "computers.txt") 

To be

foreach ($computer in Get-Content "C:\computers.txt") 

or the path where your computers.txt is located

Free Windows Admin Tool Kit Click here and download it now
May 7th, 2014 7:04pm

Hi,

This will read an input file (servers.txt) and spit out a CSV file with the results:

Get-Content .\servers.txt | ForEach {

    $computerName = $_

    If (Test-Connection -ComputerName $computerName -Count 1 -Quiet) {

        $uptime = Get-WmiObject Win32_OperatingSystem -ComputerName $computerName
        $bootTime = $uptime.ConvertToDateTime($uptime.LastBootUpTime)
        $elapsedTime = (Get-Date) - $bootTime

        $props = @{
            ComputerName = $computerName
            BootTime = $bootTime
            ElapsedTime = '{0:00} Days, {1:00} Hours, {2:00} Minutes, {3:00} Seconds' -f $elapsedTime.Days, $elapsedTime.Hours, $elapsedTime.Minutes, $elapsedTime.Seconds
        }

        New-Object PsObject -Property $props

    } Else {

        $props = @{
            ComputerName = $computerName
            BootTime = 'ERROR - Did not reply to ping'
            ElapsedTime = 'N/A'
        }

        New-Object PsObject -Property $props

    }

} | Sort ComputerName | Select ComputerName,BootTime,ElapsedTime | Export-Csv .\uptimes.csv -NoTypeInformation

May 7th, 2014 7:23pm

I have already had computers.txt in C:\ directly otherwise it won't run.

I also notice I have to change powershell directory to c:\ directly.

It is C:\Windows\System32 before.

Free Windows Admin Tool Kit Click here and download it now
May 7th, 2014 9:00pm

Mike's script is wonderful.

But how can I pull computers.txt from d drive? Can I just change to d:\computers.txt?

May 7th, 2014 9:07pm

Mike's script is wonderful.

But how can I pull computers.txt from d drive? Can I just change to d:\computers.txt?


Yep, just use Get-Content D:\computers.txt instead of Get-Content .\servers.txt.
Free Windows Admin Tool Kit Click here and download it now
May 7th, 2014 9:12pm

HI Mike,

How would you  induce the paramters if the txt file contains mix of servers in domain and workgroup.Is there a way to omit or mark the servers in workgroup while the script runs.

February 10th, 2015 1:14am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics