-
Notifications
You must be signed in to change notification settings - Fork 1
/
GetRebootTime.ps1
52 lines (41 loc) · 1.07 KB
/
GetRebootTime.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
#REQUIRES -Version 2.0
<#
.SYNOPSIS
Get last reboot for a list of servers
.DESCRIPTION
Get last reboot for a list of servers defined in a CSV file
The CSV file format is as follows:
Name
MyServer1
MyServer2
.PARAMETER file
Name of the CSV file specifying the servers (required)
.NOTES
File Name : GetRebootTime.ps1
Author : Allan Scullion
Prerequisite : PowerShell V2, ActiveDirectory module
.EXAMPLE
.\GetRebootTime.ps1 -file Test.csv
#>
param (
[parameter(Mandatory=$true)]
[alias("f")]
[string]$file = $(throw "-file is required.")
)
$dataSource = Import-Csv $file
# Loop over every computer in the list and use WMI to get the reboot time
foreach($dataRecord in $dataSource)
{
$server = $dataRecord.Name
try
{
$wmi = Get-WmiObject -Class Win32_OperatingSystem -Computer "$server"
$bootTime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
$bootTime = Get-Date $bootTime -Format 'yyyy-MM-dd hh:mm:ss'
"$server,$bootTime"
}
catch
{
Write-Host "$($server) :: Fail" -ForegroundColor Red -BackgroundColor Black
}
}