-
Notifications
You must be signed in to change notification settings - Fork 746
Expand file tree
/
Copy pathGet-Uptime.ps1
More file actions
173 lines (148 loc) · 6.39 KB
/
Get-Uptime.ps1
File metadata and controls
173 lines (148 loc) · 6.39 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
function Get-Uptime {
<#
.SYNOPSIS
The function Get-Uptime will get uptime of a local or remote machine.
.DESCRIPTION
The function Get-Uptime will get uptime of a local or remote machine.
This function is compatible with CIM sessions and alternative credentials.
.PARAMETER ComputerName
Specifies the computername
.PARAMETER Credential
Specifies the credential to use
.PARAMETER CimSession
Specifies one or more existing CIM Session(s) to use
.EXAMPLE
PS C:\> Get-Uptime -ComputerName DC01
.EXAMPLE
PS C:\> Get-Uptime -ComputerName DC01 -Credential (Get-Credential -cred "FX\SuperAdmin")
.EXAMPLE
PS C:\> Get-Uptime -CimSession $Session
.EXAMPLE
PS C:\> Get-Uptime -CimSession $Session1,$session2,$session3
.NOTES
Francois-Xavier Cat
@lazywinadmin
lazywinadmin.com
.LINK
https://github.com/lazywinadmin/PowerShell
#>
[CmdletBinding()]
PARAM (
[Parameter(
ParameterSetName = "Main",
ValueFromPipeline = $True,
ValueFromPipelineByPropertyName = $True)]
[Alias("CN", "__SERVER", "PSComputerName")]
[String[]]$ComputerName = $env:COMPUTERNAME,
[Parameter(ParameterSetName = "Main")]
[Alias("RunAs")]
[PSCredential]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty,
[Parameter(ParameterSetName = "CimSession")]
[Microsoft.Management.Infrastructure.CimSession[]]$CimSession
)
BEGIN {
# Helper Function
function Get-DefaultMessage {
<#
.SYNOPSIS
Helper Function to show default message used in VERBOSE/DEBUG/WARNING
.DESCRIPTION
Helper Function to show default message used in VERBOSE/DEBUG/WARNING
and... HOST in some case.
This is helpful to standardize the output messages
.PARAMETER Message
Specifies the message to show
.NOTES
Francois-Xavier Cat
lazywinadmin.com
@lazywinadmin
#>
PARAM ($Message)
$DateFormat = Get-Date -Format 'yyyy/MM/dd-HH:mm:ss:ff'
$FunctionName = (Get-Variable -Scope 1 -Name MyInvocation -ValueOnly).MyCommand.Name
Write-Output "[$DateFormat][$FunctionName] $Message"
}#Get-DefaultMessage
}
PROCESS {
IF ($PSBoundParameters['CimSession']) {
FOREACH ($Cim in $CimSession) {
$CIMComputer = $($Cim.ComputerName).ToUpper()
TRY {
# Parameters for Get-CimInstance
$CIMSplatting = @{
Class = "Win32_OperatingSystem"
CimSession = $Cim
ErrorAction = 'Stop'
ErrorVariable = "ErrorProcessGetCimInstance"
}
Write-Verbose -Message (Get-DefaultMessage -Message "$CIMComputer - Get-Uptime")
$CimResult = Get-CimInstance @CIMSplatting
# Prepare output
$Uptime = New-TimeSpan -Start $($CimResult.lastbootuptime) -End (Get-Date)
$Properties = [Ordered]@{
ComputerName = $CIMComputer
Days = $Uptime.days
Hours = $Uptime.hours
Minutes = $Uptime.minutes
Seconds = $Uptime.seconds
LastBootUpTime = $CimResult.lastbootuptime
}
# Output the information
New-Object -TypeName PSObject -Property $Properties
}
CATCH {
Write-Warning -Message (Get-DefaultMessage -Message "$CIMComputer - Something wrong happened")
IF ($ErrorProcessGetCimInstance) { Write-Warning -Message (Get-DefaultMessage -Message "$CIMComputer - Issue with Get-CimInstance") }
Write-Warning -Message $Error[0].Exception.Message
} #CATCH
FINALLY {
$CIMSplatting.Clear() | Out-Null
}
} #FOREACH ($Cim in $CimSessions)
} #IF ($PSBoundParameters['CimSession'])
ELSE {
FOREACH ($Computer in $ComputerName) {
$Computer = $Computer.ToUpper()
TRY {
Write-Verbose -Message (Get-DefaultMessage -Message "$Computer - Test-Connection")
IF (Test-Connection -Computer $Computer -count 1 -quiet) {
$Splatting = @{
Class = "Win32_OperatingSystem"
ComputerName = $Computer
ErrorAction = 'Stop'
ErrorVariable = 'ErrorProcessGetWmi'
}
IF ($PSBoundParameters['Credential']) {
$Splatting.credential = $Credential
}
Write-Verbose -Message (Get-DefaultMessage -Message "$Computer - Getting Uptime")
$result = Get-WmiObject @Splatting
# Prepare output
$HumanTimeFormat = $Result.ConvertToDateTime($Result.Lastbootuptime)
$Uptime = New-TimeSpan -Start $HumanTimeFormat -End $(Get-Date)
$Properties = [Ordered]@{
ComputerName = $Computer
Days = $Uptime.days
Hours = $Uptime.hours
Minutes = $Uptime.minutes
Seconds = $Uptime.seconds
LastBootUpTime = $HumanTimeFormat
}
# Output the information
New-Object -TypeName PSObject -Property $Properties
}
}
CATCH {
Write-Warning -Message (Get-DefaultMessage -Message "$$Computer - Something wrong happened")
IF ($ErrorProcessGetWmi) { Write-Warning -Message (Get-DefaultMessage -Message "$Computer - Issue with Get-WmiObject") }
Write-Warning -MEssage $Error[0].Exception.Message
}
FINALLY {
$Splatting.Clear()
}
}#FOREACH
} #ELSE (Not CIM)
}#PROCESS
}#Function