Get-WindowsUpTime.ps1


<#PSScriptInfo
 
.VERSION 1.0.3
 
.GUID ebc672ba-a2f9-48d1-85ff-aaf813d6d9cd
 
.AUTHOR saw-friendship
 
.COMPANYNAME
 
.COPYRIGHT saw-friendship
 
.TAGS Windows WMI Remote UpTime StartTime Sytem
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 Get Windows UpTime StartTime and LocalTime by Wmi on local and remote system
 
.LINK
 https://sawfriendship.wordpress.com/
 
.EXAMPLE
 Get-WindowsUpTime
 
.EXAMPLE
 Get-WindowsUpTime FileServer01 -Credential (Get-Credential)
 
.EXAMPLE
 Get-WindowsUpTime FileServer01,FileServer02
  
 
#>
 

[CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][Alias("Host","DNSHostName","IP","IPAddress")][string[]]$ComputerName = $env:COMPUTERNAME,
        [PSCredential]$Credential
    )
    
    $ComputerCount = $ComputerName.count
    [int]$i = 0
    $ComputerName | % {
    if($ComputerCount -gt 1){Write-Progress -Activity $_ -PercentComplete ($i / $ComputerCount * 100)}
    $i += 1
        if($_ -eq $env:COMPUTERNAME){
                try{
                    $OperatingSystem = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $_ -ErrorVariable WmiRequestError
                    } Catch {$WmiRequestError; break}
            } else {
                try{
                    $OperatingSystem = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $_ -Credential $Credential -ErrorVariable WmiRequestError
                    } Catch {$WmiRequestError; break}
        }
        
        if($OperatingSystem -and !$WmiRequestError){
            $LastBootUpTime = [System.Management.ManagementDateTimeconverter]::ToDateTime($OperatingSystem.LastBootUpTime)
            $LocalDateTime = [System.Management.ManagementDateTimeconverter]::ToDateTime($OperatingSystem.LocalDateTime)
            
            
            [pscustomobject][ordered]@{
                'ComputerName' = $OperatingSystem.CSName
                'CurrentTimeZone' = $OperatingSystem.CurrentTimeZone/60
                'UpTime' = $LocalDateTime - $LastBootUpTime
                'LastBootUpTime' = $LastBootUpTime
                'LocalDateTime' = $LocalDateTime
            }
        }
        $OperatingSystem = $Null
        $WmiRequestError = $Null
        $LastBootUpTime = $Null
        $LocalDateTime = $Null
        
    }