Check-Uptime-DC-Prompt.ps1

Sometimes a simple script makes life easier…

'Check-Uptime-DC-Prompt.ps1'
# Import the Active Directory module
Import-Module ActiveDirectory
 
# Get all domain controllers, excluding the specific one
$dc = read-host -prompt "What DC to check uptime on?"
 
# Initialize an array to store the results
$results = @()
 
#Get the results and format the output and add to the array
    try {
        $uptime = Invoke-Command -ComputerName $dc -ScriptBlock {
            (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime
        } -SessionOption $sessionOption -ErrorAction Stop
 
        $uptimeFormatted = (Get-Date) - $uptime
        $results += [PSCustomObject]@{
            'Domain Controller' = $dc
            'Last Boot Time'     = $uptime
            'Uptime (Days)'      = $uptimeFormatted.Days
            'Uptime (Hours)'     = $uptimeFormatted.Hours
            'Uptime (Minutes)'   = $uptimeFormatted.Minutes
        }
    } catch {
        Write-Warning "Failed to retrieve uptime for $dc $_"
    }
 
    #Output the uptime
    $results
1Though this is for a DC any server can be inputted you have rdp (WinRm) access to
 
  1. ↩︎


Posted

in

,

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.