I’m currently modifying a script written by a previous employee, and the main purpose of the script is to restart some servers. Currently, the portion of the script that actually goes through the restarts looks like this:
`
#For privacy reasons, the actual file path is replaced with “File Path”
Clear-Host
$YN = Read-Host -Prompt “CONFIRM REBOOT [Y/N]”
if(($YN -eq "Y") -or ($YN -eq "y"))
{
Get-ChildItem -Path "\File PathServer RestartsLogsRestart Logs" -Recurse -File | Move-Item -Destination "\File PathServer RestartsLogsRestart LogsArchive" -Force
$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path
$Log = New-Item "\File PathServer RestartsLogsRestart Logs$(Get-Date -f yyyy-MM-dd_hh-mm-ss) Restarts.txt" -ItemType File -Force
$Date = Get-Date -Format "dd-MMM-yyyy hh:mm:ss"
$Now = [datetime]::Now
Clear-Host
Write-Host `n `n `n
Write-Host "Rebooting all servers and verifying reboot status..." `n `n
Write-Host "Please standby, process may take up to 30 minutes..." `n `n
#Below, $Servers is declared elsewhere, pointing to a .txt file with a list of server names
Restart-Computer -ComputerName $Servers -Wait -For PowerShell -Delay 2 -Force -Timeout 1800
"----------------------------------Script executed on $Date----------------------------------" + "`r`n" | Out-File $Log -Append
foreach($Server in $Servers)
{
$LastBoot = (Get-CimInstance Win32_OperatingSystem -ComputerName $Server -EA 0).LastBootUpTime
$PingRequest = Test-Connection -ComputerName $Server -Count 1 -Quiet
if(($PingRequest -eq $true) -and ($LastBoot -gt $Now))
{
Add-Content -Path $Log -Value "$Server`: Reboot Successful."
}
elseif ($PingRequest -eq $false)
{
Add-Content -Path $Log -Value "$Server`: Server not detected, please confirm"
}
else
{
Add-Content -Path $Log -Value "$Server`: Restart not detected, please restart server manually"
}
}
Clear-Host
Write-Host `n `n `n
Write-Host "All done!" `n
Write-Host "Please review logs, as script may have run into problems." `n `n
Log-Location
Pause
}
else
{
Clear-Host
Write-Host `n `n `n
Write-Host "Server Reboots Aborted." `n `n
Pause
}
`
Currently, upon confirmation from user, it will take the log from the previous restarts and move that into an archive folder. After moving the previous log, it will create a new log in it’s place, and figure out the current time ($Now). After that, it restarts the servers, then pings them, comparing last boot up time to $Now, and puts the results in the log.
When recently running the script, I kept pinging the first server on the list with command prompt to see if it was working in real time. As the script ran, I was able to ping it, then I wasn’t able to ping it for a moment, then I was able to ping it again, so I know that at least first server restarted. However, when I checked the log after the script timed-out, not only did it display only 5 servers out of 13 total, it said “Restart not detected, please restart server manually” for all five it logged, including the one I tracked.
Based on my understanding, the script should be restarting all the servers (which it appears to be doing) and once it’s done doing that, compare the boot time to $Now (which boot time should be greater than $Now) and ping each server. The things I’m not understanding are as follows:
-
Why didn’t the first server get logged as “Reboot Successful” when I know for certain the server was restarted?
-
Why is the output log only displaying 5 of the 13 servers and not all 13?
Is there something obvious I’m missing here, or do I need to heavily rework this portion of the script?