0

所以我终于让我的脚本工作了。耶耶!!!感谢所有帮助解决此问题的人.. 但现在我有一些问题希望您能提供一些答案

问题一

Get-ADComputer 我的ForEach-Object循环中,每次迭代时都会创建一个到 Active Directory 的新连接吗?我注意到如果你单独运行命令,我需要几秒钟来连接到 AD 并获取数据。有没有更好的方法(减少连接开销)?

$Do = $Clients | ForEach-Object -Parallel {
        $O = get-ADComputer -Identity $_ -Properties Enabled

问题二 这是否也影响到之前的问题?

if (Test-Connection -ComputerName $_ -Count 2 -Quiet){  
    $ob = Get-HotFix -ComputerName $_ -Description 'Security Update'  | Select-Object -Last 1;

因此,即使我并行运行,我注意到从 200 台 PC 检索数据需要超过 50 分钟这是否正常?

输出数据

"Name","Description","KbPatch","Installed"
"PC-l146","PC Is Not Online","0","0"
"PC-l230","PC Is Not In AD or has been DISABLED","0","0"
"PC-l148","PC Is Not In AD or has been DISABLED","0","0"
"PC-l281","PC Is Not In AD or has been DISABLED","0","0"
"PC-L158","Security Update","KB4586786","11/25/2020 12:00:00 AM"
"PC-G028","Security Update","KB4570333","9/23/2020 12:00:00 AM"
"PC-l072","PC Is Not Online","0","0"
"PC-L279","Security Update","KB4577668","10/29/2020 12:00:00 AM"
"PC-l021","PC Is Not Online","0","0"
"PC-l035","PC Is Not In AD or has been DISABLED","0","0"
"PC-g005","PC Is Not In AD or has been DISABLED","0","0"
"PC-L068","Security Update","KB4580325","10/22/2020 12:00:00 AM"

我的脚本最终形式

Function Get-FileName{
    [System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) | Out-Null
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = Get-Location
    $OpenFileDialog.filter = “All files (*.*)| *.*”
    $OpenFileDialog.ShowDialog() | Out-Null
    $OpenFileDialog.filename
}

$Importfile = Get-FileName
$Clients = Get-Content $Importfile

$Do = $Clients | ForEach-Object -Parallel {
    $O = get-ADComputer -Identity $_ -Properties Enabled
    try {
        if ($O."Enabled" -eq 'TRUE') {
            #CHECK IF PC IS  NETWORK ACCESSIBLE 
            try{ 
                if (Test-Connection -ComputerName $_ -Count 2 -Quiet){  
                    $ob = Get-HotFix -ComputerName $_ -Description 'Security Update'  | Select-Object -Last 1;
                    #GET  LATEST KB PATCH INSTALLED\
                    [pscustomobject]@{
                        #"CSName","Description","HotFixID","InstalledBy","InstalledOn"
                        Name =$ob.CSName ;
                        Description = $ob.Description;
                        KbPatch = $ob.HotFixID;
                        Installed = $ob.InstalledOn;
                    }
                }
                else{
                    throw "$_,PC Is Not Online";
                }
            }
            catch { 
                $Err = $_.Exception.Message.split(',')
                [pscustomobject]@{Name =$Err[0]; Description = $Err[1]; KbPatch =0; Installed =0}
            } 
        }
        else {
            throw "$_,PC Is Not In AD or has been DISABLED"; 
        }
    }
    catch {
        $Err = $_.Exception.Message.split(',')
        [pscustomobject]@{Name =$Err[0]; Description = $Err[1]; KbPatch =0; Installed =0}
    }
}
$Do | Export-Csv -path ./OutputFile.csv -Delimiter ';' -NoTypeInformation 
4

0 回答 0