为什么 powershell 流重定向不适用于 .Net 调用?我具体尝试以下操作:
[system.net.dns]::Resolve("bla") 2>$null
这应该将错误输出重定向到 null,但事实并非如此。有很多其他方法可以抑制错误,但为什么重定向不起作用?如何重定向 .net 输出?
最佳答案
There's tons of other ways to suppress the error, but why isn't redirection working? How do you redirect .net output?
将表达式放入 PowerShell 语言结构中,例如脚本 block :
&{ [System.Net.Dns]::Resolve('bla') } 2>$null
表达式的输出仍将按预期运行:
PS> $IPHost = &{ [System.Net.DNS]::Resolve('bla') } 2>$null
PS> $IPHost -eq $null
True
PS> $IPHost = &{ [System.Net.DNS]::Resolve('www.stackoverflow.com') } 2>$null
PS> $IPHost
HostName Aliases AddressList
-------- ------- -----------
stackoverflow.com {www.stackoverflow.com} {104.16.35.249, 104.16.33.249, 104.16.37.249, 104.16.36.249...}
关于.net - 执行 .net 方法时如何重定向 Powershell 中的错误输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35169045/