From e148e6df9e621f81d2a8bca01a34fa486e546348 Mon Sep 17 00:00:00 2001 From: djorgensen Date: Thu, 6 Nov 2025 16:28:28 -0700 Subject: [PATCH] Add uptime.ps1 --- uptime.ps1 | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 uptime.ps1 diff --git a/uptime.ps1 b/uptime.ps1 new file mode 100644 index 0000000..6ca2ea6 --- /dev/null +++ b/uptime.ps1 @@ -0,0 +1,55 @@ +Function Get-Uptime { + [CmdletBinding()] + Param ( + [Parameter( + ValueFromPipeline=$true, + ValueFromPipelineByPropertyName=$true, + Position=0)] + + [string[]] + $ComputerName = $env:COMPUTERNAME + ) + + BEGIN {} + + PROCESS { + Foreach ($Computer in $ComputerName) { + $Computer = $Computer.ToUpper() + Try { + $OS = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer -ErrorAction Stop + $Uptime = (Get-Date) - $OS.ConvertToDateTime($OS.LastBootUpTime) + [PSCustomObject]@{ + ComputerName = $Computer + LastBoot = $OS.ConvertToDateTime($OS.LastBootUpTime) + Uptime = ([String]$Uptime.Days + " Days " + $Uptime.Hours + " Hours " + $Uptime.Minutes + " Minutes") + } + + } catch { + [PSCustomObject]@{ + ComputerName = $Computer + LastBoot = "Unable to Connect" + Uptime = $_.Exception.Message.Split('.')[0] + } + + } finally { + $null = $OS + $null = $Uptime + } + } + } + + END {} + +} + +$allComputers=Get-Content -Path C:\temp\pc.txt +foreach($computer in $allComputers) +{ + +$Reboots = Get-Uptime -ComputerName $computer + +#Sorting by Lastboot time. +$Reboots | Sort-Object LastBoot + + +} \ No newline at end of file