0

I will create (with PowerShell script) a table and add the result(Positive/negative) to it. I have a text file computers.txt, in which all PCs are listed.

Like this

CSNAME          Hotfixinfo
PC1             is installed
PC2             is not installed
PC3             is installed
etc.

With my actual script I can only see the positive result.

Get-Content .\computers.txt | Where {
    $_ -and (Test-Connection $_ -Quiet)
} | foreach {
    Get-Hotfix -Id KB4012212 -ComputerName $_ |
        Select CSName,HotFixID |
        ConvertTo-Csv |
        Out-File "C:\$_.csv"
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Qsec
  • 11
  • 4
  • 1
    You are filtering out _some_ bad results with `(Test-Connection $_ -Quiet)` and the rest with `Get-Hotfix -id KB4012212 -computername $_` as nulls wont be passed down the pipt . If you want to see more you need to run those commands for each computer and save the results. Check the results and it they do not have any you could pass an empty object or something like [pscustomobject]@{CSName=$_;HotFixID="N/A"} – Matt Sep 25 '17 at 12:45
  • Your `computers.txt` is a `TSV` file? – Maximilian Burszley Sep 25 '17 at 15:16

1 Answers1

0

I'd suggest parsing through and handling the positive and negative results (also faster than the pipeline ForEach-Object):

:parse ForEach ($Computer in (Get-Content C:\Path\computers.txt))
{
    If (Test-Connection $Computer -Quiet)
    {
        $Result = Get-Hotfix -Id KB4012212 -ComputerName $Computer -ErrorAction 'SilentlyContinue'

        If ($Result)
        {
            $Result |
              Select-Object -Property CSName,HotFixID |
              ConvertTo-Csv -NoTypeInformation |
              Out-File "C:\$Computer.csv"
            Continue :parse
         }
         "`"CSName`",`"HotFixID`"`r`n`"$Computer`",`"NUL`"" |
           Out-File "C:\$Computer.csv"
    } Else { Write-Host 'Unable to connect to $Computer' }
}
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63