将防病毒信息导出到PowerShell中的file.csv中



我试图获取防病毒信息并将其导出到文件.csv中。在AVName结果中,我得到了system.object[],因为它是一个数组。有什么办法吗?

Get-Content "C:Tempcomputers.txt"  | ForEach-Object {
$av_name=$null
$errorMsg=''
Try {
$av_name = (Get-CimInstance -Namespace root/SecurityCenter2 -ClassName AntivirusProduct).displayName
}
catch {
$errorMsg=$_.Exception.Message
}
if(!$av_name) {
$av_name = "The machine is not protected $errorMsg"
$av_name = "The machine is not protected"
}
else {
$av_name = (Get-CimInstance -Namespace root/SecurityCenter2 -ClassName AntivirusProduct).displayName
}
New-Object -TypeName PSObject -Property @{
ComputerName = $_
AVName = $av_name
} | Select-Object ComputerName,AVName | Export-Csv "C:TempAV_Details.csv" -NoTypeInformation
}

基本上,错误在于Export-Csvcmdlet是一个太高的闭合花括号
此外,不需要执行两次(Get-CimInstance -Namespace root/SecurityCenter2 -ClassName AntivirusProduct).displayName,并且在输出仅具有随后选择的两个属性的对象时也不需要使用Select-Object

尝试

Get-Content "C:Tempcomputers.txt"  | ForEach-Object {
# store this in a variable, because $_ will become the exception object inside the catch when things go wrong
$computer = $_  
$av_name  = $null
$errorMsg = ''
Try {
$av_name = (Get-CimInstance -Namespace root/SecurityCenter2 -ClassName AntivirusProduct -ComputerName $computer).displayName -join '; '
}
catch {
$errorMsg = $_.Exception.Message
}
if(!$av_name) {
$av_name = "The machine is not protected $errorMsg"
}
[PsCustomObject]@{
ComputerName = $computer
AVName       = $av_name
}
} | Export-Csv "C:TempAV_Details.csv" -NoTypeInformation

在我的机器上,有一个只包含IP地址的computers.txt文件,我得到的输出如下:

"ComputerName","AVName"
"192.168.0.10","Windows Defender"

转换为Csv

在此处添加

} | Select-Object ComputerName,AVName |*AddMe*| Export-Csv "C:TempAV_Details.csv" -NoTypeInformation

最新更新