gpt4 book ai didi

powershell - Powershell报告以列出修补程序

转载 作者:行者123 更新时间:2023-12-03 00:49:31 24 4
gpt4 key购买 nike

我有一个csv文件,当用户在独立环境中应用修补程序后,我将其解析出来以获取KB信息,以用于查询是否存在修补程序。我已经成功完成了查询工作(大部分时间),但是我希望输出更整洁。

该代码非常基本:

    $array = Import-Csv .\2K8R2Patches.csv -header Patchname 
foreach ($patch in $array){
$PatchID = $patch -split('-') | Select-String -Pattern "KB"
$HotFix = Get-HotFix -Id $PatchID -ErrorAction 0;

if ($HotFix)
{

Get-HotFix -Id $PatchID | Format-Table HotFixID, Description, InstalledOn -AutoSize | Out-File -Append .\PatchInstalled.txt

}

if (-Not ($HotFix))
{
Write-Host "Patch" $PatchID "is NOT installed" | Out-File -Append .\PatchInstalled.txt

}

}

对于已安装的修补程序,输出文件如下所示:

HotFixID说明InstalledOn
-------- ----------- -----------
KB3087038安全更新9/21/2015 12:00:00 AM

HotFixID说明InstalledOn
-------- ----------- -----------
KB3069114安全更新9/21/2015 12:00:00 AM

HotFixID说明InstalledOn
-------- ----------- -----------
KB3074543安全更新9/21/2015 12:00:00 AM

HotFixID说明InstalledOn
-------- ----------- -----------
KB3084135安全更新9/21/2015 12:00:00 AM

HotFixID说明InstalledOn
-------- ----------- -----------
KB3087039安全更新9/21/2015 12:00:00 AM

HotFixID说明InstalledOn
-------- ----------- -----------
KB3087918安全更新9/21/2015 12:00:00 AM

如您所见-数据就在那儿,但我希望它能变得更表格化,因此我一直试图弄清楚那部分内容。

最佳答案

您可以创建一个对象,然后将输出导出到csv或...

    $array = Import-Csv .\2K8R2Patches.csv -header Patchname
$patches = @()
foreach ($patch in $array) {
$PatchID = $patch -split('-') | Select-String -Pattern "KB"
$HotFix = Get-HotFix -Id $PatchID -ErrorAction 0;

if ($HotFix)

{
$object = New-Object -TypeName psobject -Property @{

HotFixID = $HotFix.HotFixID
Description = $HotFix.Description
InstalledOn = $HotFix.InstalledOn
}
$patches += $object
}
Else
{
$nopatch = New-Object -TypeName psobject -Property @{
HotFixID = $PatchID
InstalledOn = "Not Installed"
Description = "Not Installed"
}
$patches += $nopatch
}

}
$patches |select HotFixID, Description, InstalledOn| Out-File mypath\patch.txt

结果是,如果未安装KB,则会写成“未安装”

enter image description here

关于powershell - Powershell报告以列出修补程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33528753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com