-
Notifications
You must be signed in to change notification settings - Fork 1
/
Get-LastBootTime.ps1
43 lines (41 loc) · 1.73 KB
/
Get-LastBootTime.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
Function Get-LastBootTime {
Param(
[Parameter(ValueFromPipeline=$True)]
[String[]]$ComputerName,
[PSCredential]$Credential,
[switch]$Passthru
)
BEGIN{
If ($Computername -eq $Null){
write-verbose "No computername provided, running against local system"
}
}
PROCESS{
If ($ComputerName){
Foreach ($Computer in $ComputerName){
write-verbose "Processing Computer $Computer"
If ($Credential){
$BootTime = Get-WMIObject Win32_OperatingSystem -ComputerName $Computer -Credential $credential | select -ExpandProperty lastbootuptime
} else {
$BootTime = Get-WMIObject Win32_OperatingSystem -ComputerName $Computer | select -ExpandProperty lastbootuptime
}
If ($BootTime){
write-verbose "computer: $computer Boottime: $boottime"
If ($Passthru){
write-verbose "Passthru switch enabled, returning object with boottime and computername"
new-object psobject -Property @{
'ComputerName' = $Computer
'LastBootTime' = [System.Management.ManagementDateTimeConverter]::ToDateTime($BootTime)
}
} else {
[System.Management.ManagementDateTimeConverter]::ToDateTime($BootTime)
}
}
}
} else {
$BootTime = Get-WmiObject win32_OperatingSystem | select -ExpandProperty lastbootuptime
[System.Management.ManagementDateTimeConverter]::ToDateTime($BootTime)
}
}
END{}
}