System Up Time Since Last Reboot


While working on some tasks i came into a situation where i wanted to fetch system up time since last reboot. With help of Powershell i created a script which will check system connectivity first and then let me know the system up time in days and hours. So, i thought to share it with everyone.

Here is the script.


#*====================================================================================*#
# * Get System Up Time and Ping Status *
#*====================================================================================*#
#
# Author                :       Ashish Gupta
# Date                  :       16th April 2018
# File                  :       Get-UpTime.ps1
#
#*====================================================================================*#
# * DESCRIPTION *
#*====================================================================================*#
#
# This script will check connectivity status of all systems from the list and their
# up time depend on last reboot time stamp.
#
#*====================================================================================*#
# * Defining All Variables *
#*====================================================================================*#
#
$currentDate = Get-Date
$systems = Get-Content C:\Temp\systems.txt
#$systems = "CLIENT01", "CLIENT02"
$ping = New-Object System.Net.NetworkInformation.Ping
#
#*====================================================================================*#
# * Command to fetch ping status and Last Reboot Time *
#*====================================================================================*#
foreach ($system in $systems) {
    # * Ping status ==================================================================*#
    $Reply = $ping.send("$system")
    $printDetails = New-Object PSObject
        if ($reply.Status -eq "Success"){
            $ConnStatus = "Active"
            # * last reboot time =====================================================*#
            $lastRebootTime = Get-WmiObject Win32_OperatingSystem -ComputerName $system | Select-Object -Property @{Name='LastRestart';e={$_.ConvertToDateTime($_.LastBootUpTime)}}
            $ts = New-TimeSpan -Start $lastRebootTime.LastRestart -End $currentDate
            $upTimeDays = $ts.Days
            $upTimeHours = "$($ts.Hours):$($ts.Minutes)"
            $printDetails | Add-Member -NotePropertyName "SystemName" -NotePropertyValue $system
            $printDetails | Add-Member -NotePropertyName "Connectivity" -NotePropertyValue $ConnStatus
            $printDetails | Add-Member -NotePropertyName "UpTime(Days)" -NotePropertyValue $upTimeDays
            $printDetails | Add-Member -NotePropertyName "UpTime(HH:MM)" -NotePropertyValue $upTimeHours
            }
            else {
            # * Information aboout Inactive Systems ==================================*#
            $ConnStatus = "In-Active"
            $printDetails | Add-Member -NotePropertyName "SystemName" -NotePropertyValue $system
            $printDetails | Add-Member -NotePropertyName "Connectivity" -NotePropertyValue $ConnStatus
            $printDetails | Add-Member -NotePropertyName "UpTime(Days)" -NotePropertyValue "0"
            $printDetails | Add-Member -NotePropertyName "UpTime(HH:MM)" -NotePropertyValue "0"
            }
    $printDetails | Export-Csv C:\temp\test.csv -Append -NoTypeInformatioN
}
#*====================================================================================*#
# * End of the script *
#*====================================================================================*#



Thank You

0 comments:

Post a Comment