PowerShell log outputs wrong info

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:

  1. Why won’t the log list all of the systems on the input list?
  2. 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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật