functions/Get-RebootTime.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
<#
.Synopsis
Retrieves latest reboot time
.Description
Retrieves latest reboot time(s) of a system by event log entries
.Parameter Computer
Name of the computer to analyze
.Parameter Latest
Count of boot times to retrieve
.Inputs
None
.Outputs
None
.Example
Get-RebootTime
 
Retrieves latest reboot time of the local system
.Example
Get-RebootTime REMOTECOMPUTER 3
 
Retrieves 3 latest reboot times of the system REMOTECOMPUTER
.NOTES
Name: Get-RebootTime
Author: Markus Scholtes
Version: 1.0
Date: 2016-06-06
#>

function Get-RebootTime([STRING][Parameter(ValueFromPipeline=$TRUE)]$Computer = "$ENV:COMPUTERNAME", [INT32][ValidateScript({$_ -gt 0})]$Latest = 1)
{
    # search latest reboot events in the system log
    try {
        $EVENTS = Get-EventLog -LogName System -ComputerName $Computer -Source Microsoft-Windows-Kernel-General -InstanceId 12 -Newest $Latest
        $SCRIPT:TIMESPAN = $NULL

        # loop through found events
        foreach ($EVENT in $EVENTS)
        {
            # get time stamp
            $BOOTTIME = $EVENT.TimeGenerated
            if (!$SCRIPT:TIMESPAN)
            {
              "Last boot time of computer $Computer`: $BOOTTIME"
                $SCRIPT:TIMESPAN = New-TimeSpan -Start $BOOTTIME
                if ($($SCRIPT:TIMESPAN.Days) -lt 1)
                {
                    "Computer $Computer is active for less than a day."
                }
                else
                {
                    "Computer $Computer is active for over $($SCRIPT:TIMESPAN.Days) days."
                }
            }
            else
            {
                "Boot time of computer $Computer`: $BOOTTIME"
            }
        }
    }
    catch {
        Write-Error "Cannot connect to computer $Computer"
    }
}