Created
June 8, 2015 13:12
-
-
Save rdtechie/d87c0905380991e0d01c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#requires -Version 2 -Modules CimCmdlets | |
function Get-Uptime | |
{ | |
<# | |
.SYNOPSIS | |
Get Computer Uptime | |
.DESCRIPTION | |
Get the uptime of a Computer Object by using CIM. | |
.PARAMETER ComputerName | |
This parameter accepts multiple entries. Check the example for how to do this. | |
.INPUTS | |
This script accepts a single or array (of) strings as a input for the ComputerName parameter. | |
.OUTPUTS | |
Description of objects that are output by the script | |
.EXAMPLE | |
Retrieve uptime of a single computer object | |
Get-Uptime -ComputerName 'server01.domain.com' | |
.EXAMPLE | |
Retrieve Uptime of multiple computer objects | |
Get-Uptime -ComputerName 'server01.domain.com','server02.domain.com','server03.domain.com' | |
.EXAMPLE | |
Retrieve Uptime of multiple computer object and output to CSV | |
Get-Uptime -ComputerName 'server01.domain.com','server02.domain.com','server03.domain.com' | Export-Csv -Path 'C:\Temp\output.csv' -NoTypeInformation | |
.EXAMPLE | |
Retrieve computer server objects from Active Directory, and get the uptime | |
$adComputers = Get-ADComputer -Filter {OperatingSystem -Like "*Windows Server*"} | Select DNSHostName | |
$adComputers | ForEach-Object -Process { Get-Uptime -ComputerName $_.DNSHostName } | |
.LINK | |
http://quantica.nl | |
.NOTES | |
Version: 1.0 | |
Author: Richard Diphoorn | |
Twitter: http://twitter.com/rdtechie | |
Blog: http://powershell.org | |
Private Blog: http://quantica.nl | |
Creation Date: 2015-07-08 | |
Purpose/Change: Initial Release | |
#> | |
[CmdletBinding()] | |
Param( | |
[Parameter(ValueFromPipeline = $True)][string[]]$ComputerName | |
) | |
#region Initialization Code | |
try | |
{ | |
$computerObjects = Get-CimInstance -ComputerName $ComputerName -ClassName Win32_OperatingSystem -ErrorAction SilentlyContinue | |
} | |
catch | |
{ | |
Write-Warning -Message $_.Exception.Message | |
} | |
#endregion Initialization Code | |
#region Process Code | |
try | |
{ | |
foreach ($computerObject in $computerObjects) | |
{ | |
$upTimeDays = (New-TimeSpan -Start (Get-Date -Date $computerObject.LastBootUpTime.Ticks) -End (Get-Date)).TotalDays | |
$upTimeHours = (New-TimeSpan -Start (Get-Date -Date $computerObject.LastBootUpTime.Ticks) -End (Get-Date)).TotalHours | |
#region Constructing Output Object | |
New-Object -TypeName PSCustomObject -Property ([ordered] @{ | |
'Computername' = $computerObject.CSName | |
'LastBootTime' = $computerObject.LastBootUpTime | |
'UptimeDays' = [math]::Round($upTimeDays,0) | |
'UptimeHours' = [math]::Round($upTimeHours,1) | |
})#endregion Contructing Output Object | |
} | |
} | |
catch | |
{ | |
Write-Warning -Message $_.Exception.Message | |
} | |
#endregion Process Code | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test comment.