-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget-uptime.ps1
27 lines (22 loc) · 1.05 KB
/
get-uptime.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$serverList = Get-Content -Path "server.txt"
$outputFile = "output.csv"
# Create the output CSV file and write the header row
$header = "Hostname, Hours since last reboot, Online"
$header | Out-File -FilePath $outputFile -Encoding utf8 -Force
foreach ($server in $serverList) {
try {
# Get the uptime and calculate the hours since last reboot
$uptime = Get-Uptime -ComputerName $server
$lastReboot = (Get-Date) - $uptime
$hoursSinceReboot = [math]::Round((New-TimeSpan -Start $lastReboot).TotalHours, 2)
# Determine if the server is online
$online = Test-Connection -ComputerName $server -Count 1 -Quiet
# Build the output string and write it to the CSV file
$output = "$server, $hoursSinceReboot, $online"
$output | Out-File -FilePath $outputFile -Encoding utf8 -Append
} catch {
# If there's an error, write a message indicating that the server is offline
$output = "$server, Offline, False"
$output | Out-File -FilePath $outputFile -Encoding utf8 -Append
}
}