Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, May 7, 2014 3:39 PM
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"
}
}
All replies (6)
Wednesday, May 7, 2014 4:23 PM ✅Answered
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
Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)
Wednesday, May 7, 2014 6:12 PM ✅Answered
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.
Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)
Wednesday, May 7, 2014 4:04 PM
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
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.
Wednesday, May 7, 2014 6:00 PM
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.
Wednesday, May 7, 2014 6:07 PM
Mike's script is wonderful.
But how can I pull computers.txt from d drive? Can I just change to d:\computers.txt?
Tuesday, February 10, 2015 6:09 AM
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.
ManeeshB