Public/get-PCUpTime.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
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
function Get-PCUptime {

    <#
    .SYNOPSIS
        Displays the current uptime and last boot time for one or more computers.
 
    .DESCRIPTION
        Displays the current uptime and last boot time for one or more computers by querying CIM data. Remote computers
        are queried via WINRM using CIMInstance.
 
    .PARAMETER ComputerName
            Then name of a computer to query. Valid aliases are Name, Computer, & PC.
 
    .EXAMPLE
 
        Get-PCUptime
 
        Days : 40
        Hours : 20
        Minutes : 59
        TotalHours : 981
        LastBootTime : 4/7/2020 10:38:44 AM
 
        Returns uptime stats for the local computer
 
    .EXAMPLE
 
        Get-PCUpTime | Format-Table
 
        Days Hours Minutes TotalHours LastBootTime
        ---- ----- ------- ---------- ------------
        53 22 17 1294 4/7/2020 10:38:44 AM
 
        Returns uptime stats for the local computer and display results in table format
 
    .EXAMPLE
 
        $dc = 'DC01','DC02','DC03'
 
        Get-Uptime $dc | Format-Table -AutoSize
 
        Days Hours Minutes TotalHours LastBootTime PSComputerName RunspaceId
        ---- ----- ------- ---------- ------------ -------------- ----------
        12 0 59 289 5/19/2020 7:16:18 AM DC01 82a3b9bb-951d-4864-a285-f901ae7e5899
        105 0 25 2520 2/16/2020 7:49:49 AM DC02 732c32c4-1002-4b5e-9e9a-a541439de6d6
        205 9 57 4930 11/7/2019 10:17:50 PM DC03 114a7635-cd9d-4382-8698-4265dc4df087
 
        Returns uptime stats for three remote computers.
 
    .INPUTS
        Computername
        Accepts input from pipeline
 
    .OUTPUTS
        Output (if any)
 
    .NOTES
       NAME: Get-PCUptime.ps1
       AUTHOR: Mike Kanakos
       DATE CREATED: 2020-05-18
    #>



        [CmdletBinding()]
        param (
           [Alias("PC","Computer","Name")]
           [Parameter(
                ValueFromPipeline,
                ValueFromPipelineByPropertyName
                )]
           [string[]]
           $ComputerName = $env:COMPUTERNAME,

           [PSCredential]
           $Credential
        )

        process {
            $code = {
                $OSInfo = Get-CimInstance Win32_OperatingSystem
                $LastBootTime = $OSInfo.LastBootUpTime
                $Uptime = (New-TimeSpan -start $lastBootTime -end (Get-Date))

                $SelectProps =
                    'Days',
                    'Hours',
                    'Minutes',
                    @{
                        Name       = 'TotalHours'
                        Expression = { [math]::Round($Uptime.TotalHours) }
                    },
                    @{
                        Name       = 'LastBootTime'
                        Expression = { $LastBootTime }
                    }

                $Uptime | Select-Object $SelectProps
            } #End $code

            Invoke-Command -ScriptBlock $code @PSBoundParameters -ErrorAction SilentlyContinue -ErrorVariable NoConnect

            foreach($fail in $NoConnect) {
                Write-Warning "Failed to run on $($fail.TargetObject)"
            } #end foreach warning loop
        } #End Process
    } #end function