Snippet Name: Reboot_Status_Check
Tags:
Created Date: 2025-06-16 13:12:48
Last Modified Date: 2025-05-31 10:32:02
Dependencies:
DBA Notes:
Sample Output:
Title: Reboot Status Check
Author: user
i have the following PowerShell code below. I have a server that has bene issued a update and restart command, but it is taking a long time. can you help me check if it has not finished, based on the event 6005, and if it is not, give me an elapsed time in teh format dd:hh:mm:ss: $serverName = "DC2XFSSSQDV01"
# Get the latest event with ID 1074 from the System log
$shutdownevent = Get-WinEvent -ComputerName $serverName -FilterHashtable @{LogName = 'System'; Id = 1074} -MaxEvents 1 # shutdown
$startupvent = Get-WinEvent -ComputerName $serverName -FilterHashtable @{LogName = 'System'; Id = 6005} -MaxEvents 1 # event log service starts
if ($shutdownevent) {
# Check if the event occurred recently (e.g., within the last 5 minutes)
$timeDifference = (Get-Date) - $shutdownevent.TimeCreated
if ($timeDifference.TotalMinutes -lt 100) {
Write-Host "Server $serverName is likely rebooting As Of " $shutdownevent.TimeCreated
} else {
Write-Host "Server $serverName is not currently rebooting."
}
} else {
Author: ChatGPT
Here's an updated and complete script for your task. It checks if the server is still in the rebooting state based on the last `6005` event (startup), compares it with the `1074` shutdown event, and calculates the elapsed time if the server has not completed the restart process:
```powershell
$serverName = "DC2XFSSSQDV01"
# Get the latest shutdown and startup events
$shutdownevent = Get-WinEvent -ComputerName $serverName -FilterHashtable @{LogName = 'System'; Id = 1074} -MaxEvents 1 # Shutdown
$startupevent = Get-WinEvent -ComputerName $serverName -FilterHashtable @{LogName = 'System'; Id = 6005} -MaxEvents 1 # Event log service starts
if ($shutdownevent) {
# Check if the last shutdown event is more recent than the last startup event
if ($startupevent -and ($shutdownevent.TimeCreated -lt $startupevent.TimeCreated)) {
Write-Host "Server $serverName has completed the restart. Last startup time: $($startupevent.TimeCreated)"
} else {
# Calculate the elapsed time since the shutdown event
$elapsedTime = (Get-Date) - $shutdownevent.TimeCreated
$formattedTime = "{0:dd\:hh\:mm\:ss}" -f $elapsedTime
Write-Host "Server $serverName is likely still rebooting. Elapsed time since shutdown: $formattedTime"
}
} else {
Write-Host "No recent shutdown event found for server $serverName."
}
```
### Explanation:
1. **Retrieve Shutdown and Startup Events**:
- `1074` indicates a shutdown event.
- `6005` indicates the system startup or the Event Log service start.
2. **Logic**:
- If the `shutdownevent` is more recent than the `startupevent`, the server is likely still rebooting.
- If the `startupevent` is more recent, the server has finished restarting.
3. **Elapsed Time Calculation**:
- The script calculates the elapsed time since the `shutdownevent` using `Get-Date` and formats it as `dd:hh:mm:ss`.
4. **Output**:
- The script outputs a message depending on whether the server is still rebooting or has already restarted.
This script should provide you with clear information about the server's reboot status and the elapsed time if it hasn't completed the process.