我试图获取防病毒信息并将其导出到file.csv中。在AVName结果中,我得到了system.object[],因为它是一个数组。有什么办法解决吗?
Get-Content "C:\Temp\computers.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:\Temp\AV_Details.csv" -NoTypeInformation
}发布于 2021-06-24 10:17:42
基本上,错误是Export-Csv cmdlet是一个关闭的卷曲括号过高。
此外,不需要执行两次(Get-CimInstance -Namespace root/SecurityCenter2 -ClassName AntivirusProduct).displayName,并且在输出对象时不需要使用Select-Object,只需要使用之后选择的两个属性。
试一试
Get-Content "C:\Temp\computers.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:\Temp\AV_Details.csv" -NoTypeInformation在我的机器上,如果computers.txt文件仅包含IP地址,则输出如下:
"ComputerName","AVName"
"192.168.0.10","Windows Defender"发布于 2021-06-24 09:42:55
把这个加在这里
} | Select-Object ComputerName,AVName |*AddMe*| Export-Csv "C:\Temp\AV_Details.csv" -NoTypeInformationhttps://stackoverflow.com/questions/68112701
复制相似问题