sjqwer1
sjqwer1

Reputation: 91

Modify data in Select-Object

I am seeking for help on how to modify the value in Select-Object. I've been trying to find a related posts but I can't find any. Here's my Powershell script below.

Write-Host " - Windows Update"

Install-WindowsUpdate -AcceptAll -Install -IgnoreReboot | Out-File "$TempDirectory\WindowsUpdate.log" -Force | Out-Null

$WindowsUpdateContent = Get-Content -Path "$TempDirectory\WindowsUpdate.log"

  ForEach ($WindowsUpdateLogline in $WindowsUpdateContent) {
    if ($WindowsUpdateLogline -like "*Installed*") {
            Write-Host "  - $WindowsUpdateLogline"
        } 
    }

Here's the sample data from WindowsUpdate.log.

X ComputerName Result      KB        Size Title
- ------------ ------      --        ---- -----
1 TestPC       Accepted    KB123123  65MB 2022-01 Cumulative Update Preview for .Net Framework 3.5
2 TestPC       Downloaded  KB123123  65MB 2022-01 Cumulative Update Preview for .Net Framework 3.5
3 TestPC       Installed   KB123123  65MB 2022-01 Cumulative Update Preview for .Net Framework 3.5

Here's the output that I'm getting my script.

- Windows Update
 - 3 TestPC       Installed   KB123123  65MB 2022-01 Cimulative Update Preview for .Net Framework 3.5

This is the output that I want to accomplish.

- Windows Update
 - Installed(KB123123) - 2022-01 Cimulative Update Preview for .Net Framework 3.5

Upvotes: 0

Views: 270

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 60045

This is what Mathias meant in his comment, loop over the elements of your object to filter where the Result property is equal to Installed. As for exporting, I would recommend you to use Export-Csv so that, if you need to import it back, you would get an object back instead of having to parse text.

$windowsUpdates = Install-WindowsUpdate -AcceptAll -Install -IgnoreReboot
$windowsUpdates | ForEach-Object {
    if($_.Result -eq 'Installed')
    {
        '{0}({1}) - {2}' -f $_.Result, $_.Kb, $_.Title
    }
}
$WindowsUpdates | Export-Csv "$TempDirectory\WindowsUpdate.csv" -NoTypeInformation

Upvotes: 2

Related Questions