This is a follow-up to a previous question I posted (PowerShell output log showing incorrect information). I’m in the process of modifying a script written by a previous employee, the main purpose of which is for restarting specific servers, from a txt file. The issue is, while it seems to be restarting servers without issue, it seems to mess up when writing the output log. Here’s what I have for the actual restart portion currently:
#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 PathLogsRestart Logs" -Recurse -File | Move-Item -Destination "\File PathRestart LogsArchive" -Force
$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path
$Log = New-Item "\File PathLogsRestart 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
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
{
#"Last detected boot up time is $LastBoot" added since previous question for testing
Add-Content -Path $Log -Value "$Server`: Restart not detected, please restart server manually. Last detected boot up time is $LastBoot"
}
}
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
}
When going into the log, it printed the following (only the first server name has been edited out, the rest is unedited):
----------------------------------Script executed on 06-Jun-2024 09:50:44----------------------------------
-Redacted-: Restart not detected, please restart server manually. Last detected boot up time is 06/06/2024 21:40:08
: Restart not detected, please restart server manually. Last detected boot up time is 05/21/2024 20:39:16
FakeServerNameForTesting: Server not detected, please confirm.
Last time, it displayed the first 5 servers on the list (13 in total), now it’s only displaying the first one, one that didn’t get it’s name copied over, and the fake server name. On top of that, the two servers it is showing show a boot time after the time the log shows it was created, even though it should be doing that beforehand (time-sync shouldn’t be the issue). What I don’t understand is:
- Why won’t the log list all of the systems on the input list?
- Why is
$Date
after the$LastBoot
for each server?
Am I missing something here? As far as I understand, it should be working as intended, but clearly something’s wrong, and I have no idea what that could be.