Powershell - MSExchange Set-Contact 如何在 cmdlet returns 错误消息时引发错误
Powershell - MSExchange Set-Contact How to throw an error when cmdlet returns error message
我有以下代码可以更新 MS Exchange 中的联系人记录。我的目标是,如果它无法更新记录,它会进入我的 catch 块以记录出现错误。相反,它只是在日志中返回以下内容并继续前进,而不是进入 catch 块。我是 powershell 的新手,但我假设错误信息不是 ps 异常,而是在外部 cmdlet 上报告错误。有什么方法可以检查 cmdlet 是否遇到错误,以便我可以记录 it/go 错误路径?
我的代码片段 运行:
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
您可以通过 -ErrorAction
公共参数强制停止对 cmdlet 的特定调用:
Set-Contact -Identity $msxchangeInfoIdentity -CountryOrRegion $officeCountry -ErrorAction Stop
您还可以通过修改脚本开头的 $ErrorActionPreference
变量,将此设置为当前范围内任何调用的默认行为:
$ErrorActionPreference = 'Stop'
... 或者,也可以使用 $PSDefaultParameterValues
变量使当前范围内的 特定 cmdlet 始终在出现错误时采取此行为:
$PSDefaultParameterValues['Set-Contact:ErrorAction'] = 'Stop'
我有以下代码可以更新 MS Exchange 中的联系人记录。我的目标是,如果它无法更新记录,它会进入我的 catch 块以记录出现错误。相反,它只是在日志中返回以下内容并继续前进,而不是进入 catch 块。我是 powershell 的新手,但我假设错误信息不是 ps 异常,而是在外部 cmdlet 上报告错误。有什么方法可以检查 cmdlet 是否遇到错误,以便我可以记录 it/go 错误路径?
我的代码片段 运行:
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
您可以通过 -ErrorAction
公共参数强制停止对 cmdlet 的特定调用:
Set-Contact -Identity $msxchangeInfoIdentity -CountryOrRegion $officeCountry -ErrorAction Stop
您还可以通过修改脚本开头的 $ErrorActionPreference
变量,将此设置为当前范围内任何调用的默认行为:
$ErrorActionPreference = 'Stop'
... 或者,也可以使用 $PSDefaultParameterValues
变量使当前范围内的 特定 cmdlet 始终在出现错误时采取此行为:
$PSDefaultParameterValues['Set-Contact:ErrorAction'] = 'Stop'