-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-Uptime.ps1
72 lines (55 loc) · 2.27 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
Function Get-Uptime {
[CmdletBinding()]
Param (
[Parameter(
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[string[]]
$ComputerName = $env:COMPUTERNAME,
[Switch]
$ShowOfflineComputers
)
BEGIN {
$ErroredComputers = @()
}
PROCESS {
Foreach ($Computer in $ComputerName) {
Try {
$OS = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer -ErrorAction Stop
$Uptime = (Get-Date) - $OS.ConvertToDateTime($OS.LastBootUpTime)
$Properties = @{ComputerName = $Computer
LastBoot = $OS.ConvertToDateTime($OS.LastBootUpTime)
Uptime = ([String]$Uptime.Days + " Days " + $Uptime.Hours + " Hours " + $Uptime.Minutes + " Minutes")
}
$Object = New-Object -TypeName PSObject -Property $Properties | Select ComputerName, LastBoot, UpTime
} catch {
if ($ShowOfflineComputers) {
$ErrorMessage = $Computer + " Error: " + $_.Exception.Message
$ErroredComputers += $ErrorMessage
$Properties = @{ComputerName = $Computer
LastBoot = "Unable to Connect"
Uptime = "Error Shown Below"
}
$Object = New-Object -TypeName PSObject -Property $Properties | Select ComputerName, LastBoot, UpTime
}
} finally {
Write-Output $Object
$Object = $null
$OS = $null
$Uptime = $null
$ErrorMessage = $null
$Properties = $null
}
}
if ($ShowOfflineComputers) {
Write-Output ""
Write-Output "Errors for Computers not able to connect."
Write-Output $ErroredComputers
}
}
END {}
}
$Servers = Get-Content .\Servers.txt
$GU = Get-Uptime -ComputerName $Servers
$GU.LastBoot; $GU.Uptime