2016-08-20 274 views
0

尝试使用以下命令在远程计算机上检索安装的防病毒列表。捕获错误无效命名空间“root SecurityCenter2”

Get-WmiObject -Namespace "root\SecurityCenter2" -Class AntiVirusProduct 

防病毒计算机显示安装的防病毒详细信息。如果计算机没有任何防病毒试图捕获错误并导出CSV那台电脑没有杀毒

catch { 
    Write-Warning "[ERROR] invalid namespace [$($computer)] : $_" 
    $noantivirus+=$computer 
} 
$noantivirus | out-file -FilePath c:\noantivirus.csv -Force 

没有运气

回答

1

不 但

Invalid namespace “root\SecurityCenter2” 

引发错误似乎是一个终止错误,所以它不会被try catch语句捕获。尝试catch语句只能望尘莫及终止错误,所以你需要告诉PowerShell来治疗非终止错误的终止错误使用-ErrorAction Stop这样的:

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 -Force 
+0

怎么来的我错过了:)感谢队友的欢呼声 – DisplayName