-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGet-LastBoot.ps1
54 lines (49 loc) · 1.71 KB
/
Get-LastBoot.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
#requires -version 5.1
Function Get-LastBoot {
[cmdletbinding()]
Param(
[Parameter(
Position = 0,
ValueFromPipeline,
ValueFromPipelineByPropertyName
)]
[ValidateNotNullOrEmpty()]
[string[]]$computername = $Env:COMPUTERNAME
)
Begin {
Write-Verbose "[$((Get-Date).TimeOfDay) BEGIN ] Starting $($MyInvocation.MyCommand)"
#define hashtable of parameters to splat to Get-CimInstance
$paramHash = @{
class = "Win32_OperatingSystem"
ComputerName = $Null
ErrorAction = "Stop"
Property = "LastBootUpTime", "CSName", "Caption"
}
} #begin
Process {
<#
This is a scripting ForEach loop which is
different from the ForEach-Object cmdlet.
#>
Foreach ($computer in $Computername) {
Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Processing $($Computer.ToUpper())"
$paramHash.computername = $computer
#this is an example of error handling in PowerShell
try {
$data = Get-CimInstance @paramHash
[PSCustomObject]@{
PSTypeName = "PSLastBootInfo"
Computername = $data.CSName
LastBoot = $data.LastBootUpTime
OperatingSystem = $data.Caption
}
}
catch {
Write-Warning "There was an error: $($error[0].Exception.message)"
}
} #foreach computer
} #process
End {
Write-Verbose "[$((Get-Date).TimeOfDay) END ] Ending $($MyInvocation.MyCommand)"
} #end
} #end function