【问题标题】:Powershell - MSExchange Set-Contact How to throw an error when cmdlet returns error messagePowershell - MSExchange Set-Contact 如何在 cmdlet 返回错误消息时引发错误
【发布时间】:2021-10-25 16:39:35
【问题描述】:

我有以下代码可以更新 MS Exchange 中的联系人记录。我的目标是,如果它无法更新记录,它会进入我的 catch 块以记录出现错误。相反,它只是在日志中返回以下内容并继续前进,而不是进入 catch 块。我是 powershell 的新手,但我假设错误消息不是 ps 异常,并且在外部 cmdlet 上报告错误。有什么方法可以检查 cmdlet 是否遇到错误,以便我可以记录它/进入错误路径?

我正在运行的代码的 sn-p:

try{

$msxchangeInfo = Get-Contact | Where-Object {$_.WindowsEmailAddress -eq  "$user_search_email"}

if ($msxchangeInfo)
{
write-host "$(Get-TimeStamp) $user_search_email found...."

$msxchangeInfo = Get-Contact -Filter "WindowsEmailAddress -eq '$($user_search_email)'" | 
Select-Object -Property WindowsEmailAddress, Manager, FirstName, LastName, MobilePhone, 
DisplayName, Company, Department, Identity, StreetAddress, StateOrProvince, PostalCode, 
CountryOrRegion, City, Phone


$msxchangeInfoCountryOrRegion = $msxchangeInfo | Select -ExpandProperty CountryOrRegion

    if($msxchangeInfoCountryOrRegion -ne $officeCountry -and $officeCountry -ne "")
{

/*CODE THAT IS RETURNING ERROR MESSAGE BUT NOT THROWING EXCEPTION!*/
Set-Contact -Identity $msxchangeInfoIdentity -CountryOrRegion $officeCountry}

}
}
catch
{
write-host("$(Get-TimeStamp) *****ERROR*****: ERROR ATTEMPTING TO CREATE NEW CONTACT RECORD: 
$($displayName) . CHECK ERROR BELOW")
write-host($_.Exception|format-list -force)
write-host("$(Get-TimeStamp) Adding user to error ticket list")
$errMsg = $error[0].ToString() + $error[0].InvocationInfo.PositionMessage
$errMsg -replace ',','|'
}

但是代码永远不会碰到 catch 块。它不会引发错误,它只是记录 cmdlet 错误消息并继续。 set-contact的cmdlet错误如何破解?

这是日志中的输出(如您所见,没有记录我上面提到的自定义错误消息):

Account                              Environment TenantId                             TenantDomain  AccountType     
-------                              ----------- --------                             ------------  -----------     
gggggggggggggg-gggggg-ggggggg-gg AzureCloud  gggggggggggg-ggggg-gggg-gg TestTenant.com ServicePrincipal
Cannot process argument transformation on parameter 'CountryOrRegion'. Cannot convert value "Not Found" to type 
"Microsoft.Exchange.Data.Directory.CountryInfo". Error: "The ISO-3166 2-letter country/region code or friendly name 
"Not Found" does not represent a country/region."
    + CategoryInfo          : InvalidData: (:) [Set-Contact], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Contact
    + PSComputerName        : outlook.office365.com

【问题讨论】:

  • Set-Contact ... -ErrorAction Stop
  • 我认为你必须告诉 powershell 要捕获哪些错误。此代码将捕获所有错误catch [System.Exception]
  • @MathiasR.Jessen 非常感谢!!这解决了我的问题。我很感激!

标签: powershell exception cmdlets


【解决方案1】:

您可以通过-ErrorAction 通用参数强制对 cmdlet 的特定调用在出错时停止:

Set-Contact -Identity $msxchangeInfoIdentity -CountryOrRegion $officeCountry -ErrorAction Stop

您还可以通过在脚本开头修改$ErrorActionPreference 变量,将其设置为当前范围内任何调用的默认行为:

$ErrorActionPreference = 'Stop'

... 或者,或者,使用$PSDefaultParameterValues 变量使当前范围内的特定 cmdlet 总是在出错时采取这种行为:

$PSDefaultParameterValues['Set-Contact:ErrorAction'] = 'Stop'

【讨论】:

  • 非常感谢这解决了我的问题!很好的答案!
猜你喜欢
  • 1970-01-01
  • 2020-04-27
  • 1970-01-01
  • 2012-09-19
  • 2019-10-16
  • 2017-02-27
  • 2012-09-25
  • 2016-08-22
  • 1970-01-01
相关资源
最近更新 更多