powershellhotfix

Get-hotfix on multiple computers and exporting to CSV


How do I properly use $_ in out-file? Here's my code:

get-content computers.txt | 
  Where {$_ -AND (Test-Connection $_ -Quiet)} |
    foreach { Get-Hotfix -computername $_ } | 
      Select CSName,Description,HotFixID,InstalledBy,InstalledOn |
        convertto-csv | out-file "C:\$_.csv"

I'm trying to execute a get-hotfix for all the computers listed in the text file then I want them to be exported to CSV with the computer name as the filename.


Solution

  • You need one pipeline to process the computers.txt files, and a nested one inside the foreach to process the list of hotfixes for each computer:

    get-content .\computers.txt |
      Where {$_ -AND (Test-Connection $_ -Quiet)} |
        foreach { 
          Get-Hotfix -computername $_ |
            Select CSName,Description,HotFixID,InstalledBy,InstalledOn |
              convertto-csv | out-file "C:\$_.csv" 
        }
    

    Edit: Changed computers.txt to .\computers.txt, as this is required for local paths in powershell