尝试使用以下命令检索远程计算机上已安装的防病毒软件的列表。
Get-WmiObject -Namespace "root\SecurityCenter2" -Class AntiVirusProduct 安装了防病毒软件的计算机将显示已安装的防病毒软件的详细信息。但
Invalid namespace “root\SecurityCenter2” 如果计算机没有任何防病毒程序尝试捕获错误并导出该计算机没有防病毒程序的csv,则会抛出错误
catch {
Write-Warning "[ERROR] invalid namespace [$($computer)] : $_"
$noantivirus+=$computer
}
$noantivirus | out-file -FilePath c:\noantivirus.csv -Force 不走运
发布于 2016-08-21 00:45:19
这似乎不是一个终止错误,因此它不会在try catch语句中被捕获。Try catch语句只捕获终止错误,因此需要像这样使用-ErrorAction Stop告诉PowerShell将非终止错误视为终止错误:
try
{
Get-WmiObject -Namespace "root\SecurityCenter2" -Class AntiVirusProduct -ErrorAction Stop
}
catch
{
Write-Warning "[ERROR] invalid namespace [$($computer)] : $_"
$noantivirus+=$computer
}
$noantivirus | out-file -FilePath c:\noantivirus.csv -Forcehttps://stackoverflow.com/questions/39055990
复制相似问题