首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >捕获错误无效的命名空间"root\SecurityCenter2“

捕获错误无效的命名空间"root\SecurityCenter2“
EN

Stack Overflow用户
提问于 2016-08-20 23:47:34
回答 1查看 1.9K关注 0票数 0

尝试使用以下命令检索远程计算机上已安装的防病毒软件的列表。

代码语言:javascript
复制
Get-WmiObject -Namespace "root\SecurityCenter2" -Class AntiVirusProduct 

安装了防病毒软件的计算机将显示已安装的防病毒软件的详细信息。但

代码语言:javascript
复制
Invalid namespace “root\SecurityCenter2” 

如果计算机没有任何防病毒程序尝试捕获错误并导出该计算机没有防病毒程序的csv,则会抛出错误

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

不走运

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-21 00:45:19

这似乎不是一个终止错误,因此它不会在try catch语句中被捕获。Try catch语句只捕获终止错误,因此需要像这样使用-ErrorAction Stop告诉PowerShell将非终止错误视为终止错误:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39055990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档