Need some help with output not giving what I'm expecting

I am running the following script on some remote computers to try and achieve some automation for patch updates to distribution lists.

$ServerList = 'comp1','comp2','comp3'
$ErrorActionPreference = "SilentlyContinue"
$output = 
foreach ($ComputerName in $ServerList) {
    $OS = Get-CimInstance -ClassName CIM_OperatingSystem -ComputerName $ComputerName | Select-Object -Property Caption
    $lastBootTime = $os | Select-Object @{n="LastBootUpTime";e={(([wmi]"").ConvertToDateTime($_.LastBootUpTime)).ToString("MM/dd/yyyy hh:mm:ss tt")}} | Select-Object -ExpandProperty LastBootUpTime
    $installed = (get-hotfix | Sort-Object -Property InstalledOn)
    $lastPatchInstallTime = $installed | Select-Object -ExpandProperty InstalledOn -Last 1
    $installedInPast7Days = $installed | Where-Object {$_.InstalledOn -ge [datetime]::Today.AddHours(-168)} | Select-Object @{n="Last";e={"$(($_.InstalledOn).ToString("MM/dd/yyyy hh:mm:ss tt")) - $($_.Title)"}} | Select-Object -ExpandProperty Last
    
if (Get-Item -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\PackagesPending") {
    $pendingReboot = $true
} elseif (Get-Item -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootInProgress") {
    $pendingReboot = $true
} elseif (Get-Item -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending") {
    $pendingReboot = $true
} elseif (Get-Item -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\PostRebootReporting") {
    $pendingReboot = $true
} elseif (Get-Item -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired") {
    $pendingReboot = $true
} else {
    if (($lastBootTime | Get-Date) -lt ($lastPatchInstallTime | Get-Date)) {
        $pendingReboot = $true
    } else {
        $pendingReboot = $false
    }
    }

    [PSCustomObject]@{
        Servers              = $ComputerName.ToUpper()
        OperatingSystem      = $OS.Caption
        PendingReboot        = $pendingReboot
        LastBootTime         = $lastBootTime
        LastPatchInstallTime = $lastPatchInstallTime
        InstalledPast7Days   = $installedInPast7Days
        }
    }
$output 

Servers, Operating System, LastBootTime work fine, but the others are going off of the server i’m on rather than the server I’m trying to run the script on remotely.

Servers              : comp1
OperatingSystem      : Windows Server 2016 Standard
PendingReboot        : False
LastBootTime         : 04/21/2022 12:51:07 PM
LastPatchInstallTime : 4/4/2022 12:00:00 AM
InstalledPast7Days   : 

Servers              : comp2
OperatingSystem      : Windows Server 2012 R2 Standard
PendingReboot        : False
LastBootTime         : 04/21/2022 01:34:31 PM
LastPatchInstallTime : 4/4/2022 12:00:00 AM
InstalledPast7Days   : 

Servers              : comp3
OperatingSystem      : Windows Server 2016 Standard
PendingReboot        : True
LastBootTime         : 03/18/2021 12:10:24 PM
LastPatchInstallTime : 4/4/2022 12:00:00 AM
InstalledPast7Days   : 

So i’m getting the Server name, the OS, and the last boot time from each server, but i’m not getting the last patch install time from each server. which is throwing off the Pending reboot and installed in last 7 days.

Never mind, figured out the issue.

 $installed = (get-hotfix | Sort-Object -Property InstalledOn)

Needed to be

 $installed = (get-hotfix -ComputerName $ComputerName | Sort-Object -Property InstalledOn)