Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 在DC'上获得修补程序;s和show DC';你错过了什么_Powershell_Dns_Patch - Fatal编程技术网

Powershell 在DC'上获得修补程序;s和show DC';你错过了什么

Powershell 在DC'上获得修补程序;s和show DC';你错过了什么,powershell,dns,patch,Powershell,Dns,Patch,但是,powershell脚本按原样工作;它只显示安装了它的DC,不添加缺少它的DC(补丁)。我如何添加代码来添加DC名称,即使它缺少补丁 $default_log = $env:userprofile + "\Desktop\report_dc_installed_Hotfixes.csv" $KBPatch = Read-Host "Enter the KB Number" get-hotfix -id $KBPatch -ComputerName ((get-adforest).gl

但是,powershell脚本按原样工作;它只显示安装了它的DC,不添加缺少它的DC(补丁)。我如何添加代码来添加DC名称,即使它缺少补丁

 $default_log = $env:userprofile + "\Desktop\report_dc_installed_Hotfixes.csv"
 $KBPatch = Read-Host "Enter the KB Number"
 get-hotfix -id $KBPatch -ComputerName ((get-adforest).globalcatalogs) | `select  @{name='ServerName';expression={$_.CSName}},HotFixID,InstalledBy,InstalledOn | `export-csv $default_log -append –NoTypeInformation

我会这样做[未经测试]

$default_log = $env:userprofile + "\Desktop\report_dc_installed_Hotfixes.csv"
$KBPatch = Read-Host "Enter the KB Number"

(get-adforest).globalcatalogs | % {
    $hf_installed = $true
    try {
        $hf = get-hotfix -id $KBPatch -ComputerName $_ -ErrorAction Stop 
    } catch {
        $hf_installed = $false
    }

    if ($hf_installed) {
        $hf | `select  @{name='ServerName';expression={$_.CSName}},@{name='HotfixInstalled';expression={$hf_installed}},HotFixID,InstalledBy,InstalledOn | `export-csv $default_log -append –NoTypeInformation
    } else {
        [pscustomobject] @{
            'ServerName'=$_;
            'HotfixInstalled'=$hf_installed
        } | export-csv $default_log -append –NoTypeInformation
    }
}
逻辑解释:

  • 我们需要显式地测试每个主机的成功/失败,以便循环 (foreach对象,%)
  • 我们测试在补丁不可用的情况下引发的异常 现有(try/catch with-erroraction停止我们的命令)
  • 然后,我们检查是否成功或失败,以确定 要写入日志的信息类型

  • …你为什么不直接使用原力?

    你想要输出什么?全球目录。从那里开始

    你想知道关于他们的什么?他们的名字,选择那个。他们是否安装了修补程序。选择那个

    你想把它放在哪里?在您的CSV中。把它放在那里

    (Get-ADForest).GlobalCatalogs |
         Select-Object @{Name='ServerName';      Expression={$_}},
                       @{Name="HotFixInstalled"; Expression={
                         [bool](Get-HotFix -Id $KBPatch -ComputerName $_ -EA SilentlyContinue)
                       }} |
        Export-Csv -Path $DefaultLog -Append –NoTypeInformation
    

    你怎么能做到?重写代码以迭代GC服务器列表,如果安装了修补程序,则输出
    $true
    ;如果未安装修补程序,则输出
    $false