Powershell is not properly showing the output of my array from my hashtable

71 views Asked by At

I had another question asked on this issue I'm having with Powershell. I can't get all the columns to return from an array hashtable. I'm trying to search multiple PCs for certain updates but the array only returns the last computer in the array. Please can you assist?

Previous question: Returned hashtable does not display all columns

    [CmdletBinding()]
        Param (
        [string]$path = $ENV:HOMEPATH+"\Documents",
        [string]$Version = "2008",
        [array]$HotFix = ("KB5031043","KB5031419")
    ) 
    # Get all Servers
      $filter = "*"+$Version+"*"
      $computers = @()
      Write-Verbose "Gathering computer data..."
      # $computers = @(Get-ADComputer -Filter {OperatingSystem -like $filter} -Properties *)
      $computers = @("COMP1","COMP2","COMP3","COMP4")
      # $computers = @(Get-ADComputer -Filter {OperatingSystem -like "*2008*"} -Properties * | select -First 7)
    # Import-Csv "C:\Scripts\gitScripts\Priv_CF\ESU\hosts.csv" | ForEach-Object {$computers += $_.Host}
    $inLine = @()
    $duzit = "CF3" + (Get-Date -format "yyMMdd")
    $file = $duzit + " - ESU-Computerlist-"+$version+".csv"
    $startTime = Get-Date
    # Loop through
    # $result = for ($i = 0; $i -lt $HotFix.Length; $i++) {}
    $inLine = foreach ($computer in $computers) {
        for ($i = 0; $i -lt $HotFix.Length; $i++) {
            $current = $HotFix[$i]
            $connection = ""
            #$name = $computer.Name
            $name = $computer
            $Present = "N/A"
            $WinRM = "N/A"
            $OS = $computer.OperatingSystem
            $IP = $computer.IPv4Address
            $Build = (Get-WmiObject Win32_OperatingSystem -ComputerName $name).Version
            $p1sequence = [string]$i+"/"+[string]$count
            $elapsedTime = $(get-date) - $startTime
            $elapsedTime = $elapsedTime.Minutes
            $error.Clear()
                $out = [ordered]@{
                    Name = $name
                    Connection = $connection
                    #RegValue = $RegValue.BuildLabEx
                    OS = $OS
                    IP = $IP
                    #Build = $Build
                    WinRM = $WinRM
                }
                foreach ($fix in $HotFix) {
                    if ($fix -eq $current) {
                    try {Test-Connection -ComputerName $name -ErrorAction Stop}
                        catch {$connection = "No"}
                            if (!$error) {
                                $connection = "Yes"
                            if ($WinRM = (Test-WsMan -ComputerName $name).ProductVersion) {$WinRM = "Yes"} else {$WinRM = "No"}
                                try {$RegValue = @(Invoke-Command -ComputerName $name {Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name BuildLabEx})}
                                catch {$connection = "WinRM Error"}
                            $error.clear()
                            #if (Get-HotFix -Id $HotFix -ComputerName $name -ErrorAction SilentlyContinue) {$Present = "Yes"} else {$Present = "No"}
                                try {Get-HotFix -Id $Fix -ComputerName $name -ErrorAction Stop}
                                catch {if ($error -like "*Cannot find the requested hotfix*") {$Present =  "No"} else {$Present = "WinRM Error"}}
                            if (!$error) {$Present = "Yes"}
                                $error.Clear()
                            }
                        $out[$fix] = $Present
                        continue
                    }
                    #$out[$fix] = ''
                }
                [pscustomobject]$out
            }
        $inLine += $out
        Write-Verbose ("ComputerName: " +$name)
        Write-Verbose ("Connection: " + $connection)
        Write-Verbose ("WinRM: " + $WinRM)
        Write-Verbose ($Fix +": " + $Present)
        Write-Verbose ("Elapsed Time: "+$([string]$elapsedTime) +" Minutes")
    }
    $inLine | ft #| Export-Csv -Path ($path +"\" + $file) -NoTypeInformation
    Write-Verbose ([string]$startTime + " (Run time: " + [string]$elapsedTime + " Minutes)")
    Write-Host "FILE SAVED: " $path"\"$file

I was expecting the output to display all the computers and the hot fixes for each computer. The output example should look like this:

    Computer  Connection  OS    IP    WinRM KB5031043  KB5031419
    --------   ---------   --    --    ----- ---------  ---------
    COMP1      Yes         <OS>  <IP>  Yes   Yes        No       
    COMP2      Yes         <OS>  <IP>  Yes   No         Yes
    COMP3      Yes         <OS>  <IP>  Yes   No         Yes
    COMP4      Yes         <OS>  <IP>  Yes   No         Yes
0

There are 0 answers