Scripts/Get-SystemUpTime.ps1
#requires -Version 3.0 <#PSScriptInfo .VERSION 1.7 .GUID 4f5d3d64-7d6e-407e-a902-cdbc1b6175cd .AUTHOR Erik .COMPANYNAME KnarrStudio .COPYRIGHT .TAGS .LICENSEURI .PROJECTURI https://knarrstudio.github.io/ITPS.OMCS.Tools/ .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES .PRIVATEDATA #> <# .DESCRIPTION Returns system uptime #> [CmdletBinding()] Param() function Get-SystemUpTime { <# .SYNOPSIS Short Description .DESCRIPTION Detailed Description .EXAMPLE Get-SystemUpTime explains how to use the command can be multiple lines .EXAMPLE Get-SystemUpTime another example can have as many examples as you like #> <# .SYNOPSIS Returns the last boot time and uptime in hours for one or many computers .DESCRIPTION Returns system uptime .PARAMETER ComputerName One or Many Computers .PARAMETER ShowOfflineComputers Returns a list of the computers that did not respond. .EXAMPLE Get-UpTime -ComputerName Value -ShowOfflineComputers Returns the last boot time and uptime in hours of the list of computers in "value" and lists the computers that did not respond .OUTPUTS ComputerName LastBoot TotalHours ------------ -------- ---------- localhost 10/9/2019 00:09:28 407.57 tester Unable to Connect Error Shown Below Errors for Computers not able to connect. tester Error: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) #> [cmdletbinding(DefaultParameterSetName = 'DisplayOnly')] Param ( [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName,Position = 0)] [Alias('hostname')] [string[]]$ComputerName = $env:COMPUTERNAME, [Parameter (ParameterSetName = 'DisplayOnly')] [Switch]$ShowOfflineComputers, <# [Parameter (ParameterSetName = 'DisplayOnly')] [Switch]$DisplayOnly,#> [Parameter (ParameterSetName = 'DisplayOnly')] [Switch]$BootOnly, [Parameter (ParameterSetName = 'FileOnly')] [Switch]$FileOnly, [Parameter (ParameterSetName = 'FileOnly')] [String]$OutCsv = "$env:HOMEDRIVE\Temp\UpTime.csv" ) BEGIN { $ErroredComputers = @() if($BootOnly) { $SelectObjects = 'ComputerName', 'LastBoot' } else { $SelectObjects = 'ComputerName', 'LastBoot', 'TotalHours' } if($DisplayOnly) { $OutCsv = $null } if($FileOnly) { if (Test-Path -Path $OutCsv) { $i = 1 $NewFileName = $OutCsv.Trim('.csv') Do { $OutCsv = ('{0}({1}).csv' -f $NewFileName, $i) $i++ }while (Test-Path -Path $OutCsv) } } } PROCESS { Foreach ($Computer in $ComputerName) { Try { $OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computer -ErrorAction Stop $UpTime = (Get-Date) - $OS.ConvertToDateTime($OS.LastBootUpTime) $Properties = @{ ComputerName = $Computer LastBoot = $OS.ConvertToDateTime($OS.LastBootUpTime) TotalHours = ( '{0:n2}' -f $UpTime.TotalHours) } $Object = New-Object -TypeName PSObject -Property $Properties | Select-Object -Property $SelectObjects } catch { if ($ShowOfflineComputers) { $ErrorMessage = ('{0} Error: {1}' -f $Computer, $_.Exception.Message) $ErroredComputers += $ErrorMessage $Properties = @{ ComputerName = $Computer LastBoot = 'Unable to Connect' TotalHours = 'Error Shown Below' } $Object = New-Object -TypeName PSObject -Property $Properties | Select-Object -Property $SelectObjects } } finally { if($FileOnly) { $Object | Export-Csv -Path $OutCsv -Append -NoTypeInformation Write-Verbose -Message ('Output located {0}' -f $OutCsv) } Write-Output -InputObject $Object $Object = $null $OS = $null $UpTime = $null $ErrorMessage = $null $Properties = $null } } } END { if ($ShowOfflineComputers) { Write-Output -InputObject '' Write-Output -InputObject 'Errors for Computers not able to connect.' Write-Output -InputObject $ErroredComputers } } } # SIG # Begin signature block # MIID7QYJKoZIhvcNAQcCoIID3jCCA9oCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB # gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR # AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUH1A5K7X7b9++bpYOlCYOGvm9 # BAOgggINMIICCTCCAXagAw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