0

What I am trying to achieve here is add the servers and the updates that are not installed on the server to an array and create a new object that is going to display the names of the servers in one column and the missing updates on another column, but at the end I am getting an empty Grid-View table.

The values for the servers and updates are read from a file.

Write-Host
#Read the password from stdin and store it in a variable
$password = Read-Host -AsSecureString -Prompt "Enter your password"
Write-Host
#Get credentials and password for later user
$cred = New-Object System.Management.Automation.PSCredential ("[email protected]", $password )

#Get the list of available servers to test
$servers = Get-Content -Path $HOME\Desktop\servers.txt
#Get the list of available updates that need to be installed on the server
$available_updates = Get-Content $HOME\Desktop\update.txt

$add_updates = @()
$add_updates_and_servers = @()

#Get each server name from the list and execute the following commands
foreach ($server in $servers) {

    #Test if the server is reponding
    $ping = Test-Connection $server -Count 1 -Quiet

    #If the above command returns True continue
    if ($ping -eq "True") {

        #Write a message saying Testing server_name
        Write-Host "Testing $server"
        
        foreach ($update in $available_updates) {

            #Check if update is installed
            $updates_from_os = Invoke-Command -ComputerName $server -Credential $cred -ScriptBlock { Get-HotFix | Select-Object -Property HotFixID | Where-Object -Property HotFixID -EQ $Using:update } -HideComputerName | Select-Object -ExpandProperty HotFixID

            if (!$updates_from_os) {
                
                $add_updates += $update

            }
        }

        New-Object -TypeName PSObject -Property $updates -OutVariable final
            
        $updates = @{

            "Server"  = $server
            "Updates" = $add_updates

        }
    }

    $add_updates_and_servers += $final
}

$add_updates_and_servers | Out-GridView

1 Answer 1

1

For what is probably happening with your script:
I suspect that each time you calling the statement New-Object -TypeName PSObject -Property $updates -OutVariable final You overwriting any previous created $final object which references to the same objects as your $add_updates_and_servers collection.

Anyways, try to avoid using the increase assignment operator (+=) to create a collection, instead stream the results to a variable (or even better, directly to next/final cmdlet: ... }| Out-GridView).
Something like:

$add_updates_and_servers = foreach ($server in $servers) {
    $ping = Test-Connection $server -Count 1 -Quiet
    if ($ping -eq "True") {
        Write-Host "Testing $server"
        $add_updates = @(
            foreach ($update in $available_updates) {
                $updates_from_os = Invoke-Command -ComputerName $server -Credential $cred -ScriptBlock { Get-HotFix | Select-Object -Property HotFixID | Where-Object -Property HotFixID -EQ $Using:update } -HideComputerName | Select-Object -ExpandProperty HotFixID
                if (!$updates_from_os) { $update }
            }
        )
        [PSCustomObject]@{
            "Server"  = $server
            "Updates" = $add_updates
        }
    }
}

Note: in case you want each $update in a separate column, also have a look at: Not all properties displayed

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response and help, that fixed it. Now I can see the output in a Out-GridView or even save it to a .csv file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.