修改 Select-Object 中的数据

Modify data in Select-Object

我正在寻求有关如何修改 Select-Object 中的值的帮助。我一直试图找到相关的帖子,但我找不到任何。下面是我的 Powershell 脚本。

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"
        } 
    }

这是来自 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

这是我获取脚本的输出。

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

这是我想要完成的输出。

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

希望大家帮帮忙!提前致谢!

这就是 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 这样,如果您需要将其导回,您将得到一个对象,而不必解析文本。

$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