Skip to content

Instantly share code, notes, and snippets.

@hpmmatuska
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hpmmatuska/6c7bd95e72df8f258b2d to your computer and use it in GitHub Desktop.
Save hpmmatuska/6c7bd95e72df8f258b2d to your computer and use it in GitHub Desktop.
Get Uptime for list of computers with basic errorhandling. It's processed in parallel.
Function Get-Uptime {
<#
.Synopsis
Get computer uptime.
.Description
This command will query the Win32_OperatingSystem class using Get-CimInstance and write an uptime object to the pipeline.
The function is calling workflow to parallely process all inputs to save some time.
.Parameter Computername
The name of the computer(s) to query. This parameter has an alias of CN. The Input can be piped.
.Example
PS C:\> get-myuptime pc1,pc2,pc3
ComputerName LastBootUpTime Days Hours Minutes Seconds TotalDays
------------ -------------- ---- ----- ------- ------- ---------
PC1 11.12.2014 0:27:07 49 8 35 39 49,36
PC2 27.12.2014 17:58:36 32 15 4 10 32,63
PC3 Not Accessible
Formatted results for multiple computers. You can also pipe computer names into this command.
.Notes
Last Updated: January 27, 2015
Version : 1.0
.Link
#>
[cmdletbinding()]
Param(
[Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[ValidateNotNullorEmpty()]
[Alias("cn","name")]
[String[]]$ComputerName,
[System.Management.Automation.CredentialAttribute()]$credential
)
Workflow Get-HostUptime{
Param(
[String[]]$ComputerName,
[System.Management.Automation.CredentialAttribute()]$credential
)
Foreach -parallel ($computer in $computername){
sequence{
#InlineScript {
Try {$wmi = gwmi Win32_OperatingSystem -PSComputerName $Computer -PSCredential $credential -ErrorAction Stop
$LBTime = [System.Management.ManagementDateTimeconverter]::ToDateTime($wmi.LastBootUpTime)
$uptime = [DateTime]::Now - $LBTime
$Obj = [PSCustomObject] @{
ComputerName = $Computer.ToUpper()
LastBootUpTime = $LBTime
Days = $uptime.Days
Hours = $uptime.Hours
Minutes = $uptime.Minutes
Seconds = $uptime.Seconds
TotalDays = $uptime.totaldays
}
} #try
Catch {
$Obj = [PSCustomObject] @{
ComputerName = $Computer.ToUpper()
LastBootUpTime = "Not Accessible"
}
} # catch
$Obj
#} # InlineScript
} # Sequence
} # ForEach
} #end workflow
$CurrentPath = Get-Location # to achieve no problems when workflow is called from custom mapped PSDrive
Set-Location $env:SystemRoot # cange location before workflow
if ($Credential) {Get-HostUptime -ComputerName $ComputerName -Credential $credential | sort-object -Property LastBootUpTime -Descending | Select-Object ComputerName,LastBootUpTime,Days,Hours,Minutes,Seconds, @{Name="TotalDays";Expression={"{0:N2}" -f $_.TotalDays}} | ft -autosize }
else {Get-HostUptime -ComputerName $ComputerName | sort-object -Property LastBootUpTime -Descending | Select-Object ComputerName,LastBootUpTime,Days,Hours,Minutes,Seconds, @{Name="TotalDays";Expression={"{0:N2}" -f $_.TotalDays}} | ft -autosize }
Set-Location $CurrentPath #return location to original path
} #end function uptime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment