Skip to content

Instantly share code, notes, and snippets.

@Agazoth
Last active February 18, 2018 15:36
Show Gist options
  • Save Agazoth/30dd89bde330489154c52b32fe99a765 to your computer and use it in GitHub Desktop.
Save Agazoth/30dd89bde330489154c52b32fe99a765 to your computer and use it in GitHub Desktop.
Gets the uptime from Windows and Linux machines. A possible solution for Iron Scripter Prequel 2018 Puzzle 6
function Get-UpTime {
[CmdletBinding()]
param ([parameter(ValueFromPipeline)][string]$ComputerName,[PSCredential]$Credential,$Authentication)
$Parameters = @{
ScriptBlock = [scriptblock]::Create('if($env:OS -match "Windows"){$strBoot = $((systeminfo | find "System Boot Time") -replace "^.+:\s+|,")} else {$strBoot = (who -b) -replace "^\D+"}; Get-Date $strBoot')
}
if (!$ComputerName){$ComputerName = hostname}
if ($Credential){$Parameters.Add('Credential',$Credential);$Parameters.Add('ComputerName',$ComputerName)}
if ($Authentication){$Parameters.Add('Authentication',$Authentication)}
$BootDate = icm @Parameters
[PSCustomObject]@{
ComputerName = $ComputerName
LastBootTime = $BootDate
Uptime = [math]::Round($(New-TimeSpan $BootDate).TotalDays,3)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment