如何在脚本为 运行 get-wmiobject 时抑制 PowerShell 错误。 -EA 不工作
How to suppress PowerShell errors while script is running get-wmiobject. -EA doesn't work
这是我的脚本。
get-wmiobject -class win32_networkadapterconfiguration
-computername (get-content .\computers.txt) -Filter "IPEnabled=TRUE" |
select PSComputerName,DNSServerSearchOrder
我的 Get-Content 将有一个巨大的计算机列表,其中一些不再存在于我的网络中,而另一些则存在。因此,在生成此列表时,我将获得 "RPC Server not available" 红色错误文本。我根本不想显示它。我试过 -ErrorAction SilentlyContinue
或 Ignore
,但行为没有任何变化。
我也试过这个:
try {
get-wmiobject -class win32_networkadapterconfiguration
-computername (get-content .\computers.txt) -Filter "IPEnabled=TRUE" |
select PSComputerName,DNSServerSearchOrder
}
catch {
}
这也没有给我带来好运。感谢任何帮助!
我可以通过
解决这个问题
get-wmiobject -class win32_networkadapterconfiguration
-computername (get-content .\computers.txt) 2>$null -Filter "IPEnabled=TRUE" |
select PSComputerName,DNSServerSearchOrder
而且我还在 Select 之后执行 -ErrorAction,您需要在 wmi 管道中的 select 之前执行此操作。希望这对以后像我这样的人有帮助。
您没有向我们展示您的使用方式 -ErrorAction silentlyContinue
。如果放置得当,那会抑制你的错误。我猜你把它放在命令的末尾是为了将它与 Get-WMIObject
相关联。
get-wmiobject -class win32_networkadapterconfiguration -computername (get-content .\computers.txt) -Filter "IPEnabled=TRUE" -ErrorAction SilentlyContinue |
select PSComputerName,DNSServerSearchOrder
你的 try catch 块也可以工作,但我认为它会在第一个错误时停止处理,从而跳过其他好的计算机。
这是我的脚本。
get-wmiobject -class win32_networkadapterconfiguration
-computername (get-content .\computers.txt) -Filter "IPEnabled=TRUE" |
select PSComputerName,DNSServerSearchOrder
我的 Get-Content 将有一个巨大的计算机列表,其中一些不再存在于我的网络中,而另一些则存在。因此,在生成此列表时,我将获得 "RPC Server not available" 红色错误文本。我根本不想显示它。我试过 -ErrorAction SilentlyContinue
或 Ignore
,但行为没有任何变化。
我也试过这个:
try {
get-wmiobject -class win32_networkadapterconfiguration
-computername (get-content .\computers.txt) -Filter "IPEnabled=TRUE" |
select PSComputerName,DNSServerSearchOrder
}
catch {
}
这也没有给我带来好运。感谢任何帮助!
我可以通过
解决这个问题get-wmiobject -class win32_networkadapterconfiguration
-computername (get-content .\computers.txt) 2>$null -Filter "IPEnabled=TRUE" |
select PSComputerName,DNSServerSearchOrder
而且我还在 Select 之后执行 -ErrorAction,您需要在 wmi 管道中的 select 之前执行此操作。希望这对以后像我这样的人有帮助。
您没有向我们展示您的使用方式 -ErrorAction silentlyContinue
。如果放置得当,那会抑制你的错误。我猜你把它放在命令的末尾是为了将它与 Get-WMIObject
相关联。
get-wmiobject -class win32_networkadapterconfiguration -computername (get-content .\computers.txt) -Filter "IPEnabled=TRUE" -ErrorAction SilentlyContinue |
select PSComputerName,DNSServerSearchOrder
你的 try catch 块也可以工作,但我认为它会在第一个错误时停止处理,从而跳过其他好的计算机。