それで、最終的にスクリプトが機能するようになりました。イェーイ!!! これを手伝ってくれたすべての人に感謝します..しかし、今私はいくつか質問があります.あなたがいくつかの答えを提供できることを願っています.
質問 1
私のループのGet-ADComputer
中にForEach-Object
ありますが、反復するたびに Active Directory への新しい接続を作成していますか? コマンドをスタンドアロンとして実行すると、AD に接続してデータを取得するのに数秒かかることに気付きました。より良い方法はありますか (接続のオーバーヘッドを減らします)?
$Do = $Clients | ForEach-Object -Parallel {
$O = get-ADComputer -Identity $_ -Properties Enabled
質問 2 これは、前の質問と同様に影響しますか?
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