0
即時通訊卡停留在如何將我的foreach循環輸出到csv或excel文件。此腳本只是將所有計算機關閉到本地網絡,然後測試以查看該計算機是否具有某個KBPatch。該腳本正如我所說的那樣工作,我需要幫助才能將其輸出到csv文件。任何提示/幫助表示讚賞將foreach循環放入csv文件
下面的代碼
$strCategory = "computer"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$ObjSearcher.SearchRoot = $objDomain
$objSearcher.filter = ("(objectCategory=$strCategory)")
$colProplist = "name"
foreach($i in $colProplist)
{
$objSearcher.PropertiesToLoad.Add($i)
}
#Finds all operating systems and computer names
$colResults = $objSearcher.FindAll()
foreach($objResult in $colResults)
{
$objComputer = $objResult.Properties;
$names = $objComputer.name
# Define Hotfix to check
$CheckKBS = @(「patch#" , "patch#")
#Query the computers for the HotFix
foreach($name in $names)
{
foreach ($CheckKB in $CheckKBS) {
$HotFixQuery = Get-HotFix -ComputerName $name | Where-Object {$_.HotFixId -eq $CheckKB} | Select-Object -First 1;
if($HotFixQuery -eq $null)
{
Write-Host 「Hotfix $CheckKB is not installed on $name」;
}
else
{
Write-Host 「Hotfix $CheckKB was installed on $name by 」 $($HotFixQuery.InstalledBy);
}
}}
}
不需要模板,您可以在ForEach循環的每次傳遞中構建一個新的自定義對象。你可以在你的'$ records + ='行強行輸入'[array] $ records'作爲變量,並避免事先實例化它。由於數組是如何在每次使用'+ ='的時候被銷燬和重建的,最好''array'$ Records = ForEach(){'並且一次性收集循環輸出對象而不是重建數組一遍又一遍。 – TheMadTechnician