Add uptime.ps1
This commit is contained in:
parent
d7cf4dccb3
commit
e148e6df9e
55
uptime.ps1
Normal file
55
uptime.ps1
Normal file
@ -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
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user