Read the content from text file

Hi guys,
I have created a simple script to collect the hot fix in a list of 4 computers in a text file. But when running the script, only the first computer in the list appear 4 times as below

Blockquote
PS C:> $computers = Get-Content -Path c:\test\computers.txt
PS C:> $computers | ForEach-Object {Get-hotfix | sort -Descending | select -First 5 | ft -a}

Source Description HotFixID InstalledBy InstalledOn


Server1 Security Update KB5003638 NT AUTHORITY\SYSTEM 25/06/2021 00:00:00
Server1 Security Update KB5001402 NT AUTHORITY\SYSTEM 24/06/2021 00:00:00
Server1 Security Update KB4550994 NT AUTHORITY\SYSTEM 04/06/2020 00:00:00
Server1 Security Update KB4535680 NT AUTHORITY\SYSTEM 25/06/2021 00:00:00
Server1 Security Update KB4520724 NT AUTHORITY\SYSTEM 22/01/2020 00:00:00

Source Description HotFixID InstalledBy InstalledOn


Server1 Security Update KB5003638 NT AUTHORITY\SYSTEM 25/06/2021 00:00:00
Server1 Security Update KB5001402 NT AUTHORITY\SYSTEM 24/06/2021 00:00:00
Server1 Security Update KB4550994 NT AUTHORITY\SYSTEM 04/06/2020 00:00:00
Server1 Security Update KB4535680 NT AUTHORITY\SYSTEM 25/06/2021 00:00:00
Server1 Security Update KB4520724 NT AUTHORITY\SYSTEM 22/01/2020 00:00:00

Source Description HotFixID InstalledBy InstalledOn


Server1 Security Update KB5003638 NT AUTHORITY\SYSTEM 25/06/2021 00:00:00
Server1 Security Update KB5001402 NT AUTHORITY\SYSTEM 24/06/2021 00:00:00
Server1 Security Update KB4550994 NT AUTHORITY\SYSTEM 04/06/2020 00:00:00
Server1 Security Update KB4535680 NT AUTHORITY\SYSTEM 25/06/2021 00:00:00
Server1 Security Update KB4520724 NT AUTHORITY\SYSTEM 22/01/2020 00:00:00

Source Description HotFixID InstalledBy InstalledOn


Server1 Security Update KB5003638 NT AUTHORITY\SYSTEM 25/06/2021 00:00:00
Server1 Security Update KB5001402 NT AUTHORITY\SYSTEM 24/06/2021 00:00:00
Server1 Security Update KB4550994 NT AUTHORITY\SYSTEM 04/06/2020 00:00:00
Server1 Security Update KB4535680 NT AUTHORITY\SYSTEM 25/06/2021 00:00:00
Server1 Security Update KB4520724 NT AUTHORITY\SYSTEM 22/01/2020 00:00

Blockquote

The computers.txt looks as below:
Server1
Server2
Server3
Server4

Is there anything wrong with the format of the text file? Does it need a comma between computers?

You don’t need a loop for that. The parameter -ComputerName of the cmdlet Get-Hotfix accepts an array. This should be enough:

$ComputerName = Get-Content -Path 'c:\test\computers.txt' | 
Get-HotFix -ComputerName $ComputerName | 
    Sort-Object -Property Source,HotfixID -Descending | 
        Format-Table -AutoSize

BTW: When you post code or error messages or console output please format it as code using the preformatted text button ( </> ).

Thanks in advance

2 Likes

Hi Olaf,
Big thank you for your assistance.