gpt4 book ai didi

powershell - 使用PSSession时令人困惑的ErrorAction行为

转载 作者:行者123 更新时间:2023-12-03 08:28:59 24 4
gpt4 key购买 nike

我正在编写一个脚本,该脚本将使用类似于以下内容的方式针对我的邮件服务器远程运行 :

$credentials = Get-Credential
$session = New-PSSession `
-ConnectionUri http://mailserver/PowerShell/ `
-Authentication Kerberos `
-ConfigurationName Microsoft.Exchange `
-Credential $credentials
$module = Import-PSSession $session

我将通过读取主机,参数或管道从用户那里收到收件人的姓名,因此,如果收件人无效且get-recipient不成功,我希望我的脚本可以纾困。 -ErrorAction似乎是一个合理的选择。
get-recipient "doesnotexist" -ErrorAction SilentlyContinue; write-host "will output";
get-recipient "doesnotexist" -ErrorAction Stop; write-host "will output"
get-recipient "doesnotexist" -ErrorAction Inquire; write-host "will not output if halt selected, otherwise displayed"

根据 about_CommonParameters获得帮助,-EA应该修改非终止错误的行为。这里有一个非终止错误,我想通过...终止来处理。我知道EA之所以起作用,是因为我将其设置为“查询”可以(交互地)获得期望的结果。我想要的是在没有交互的情况下终止,因此我可以尝试/捕获它。

最佳答案

为此使用-ErrorAction Stop

get-recipient "doesnotexist" -ErrorAction Stop; write-host "will not output"

关于powershell - 使用PSSession时令人困惑的ErrorAction行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31551299/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com