如何解决使用 Connect-AzAccount 连接到 Azure 时处理连接错误
我已经编写了一个 Runbook powershell 脚本,但是我在捕捉使用 Connect-AzAccount 命令收到的任何错误时遇到了问题。似乎将 Connect-AzAccount 包装在 try catch 中不起作用。剧本继续。如何处理使用此命令返回的错误
try
{
The catch is never executed even when there is an error
Connect-AzAccount -Identity;
}
catch
{
write-output "Error connecting to Azure";
write-output $_.Exception.message;
$response = .\SendEmail.ps1 -To "xxx@xxx.com" -From "xxx@xxx" -Subject "xxxx" -Message "xxxxx";
}
Unable to acquire token for tenant 'organizations'
Connect-AzAccount : ManagedIdentityCredential authentication unavailable. The requested identity has not been assigned
to this resource.
Status: 400 (Bad Request)
Content:
{"error":"invalid_request","error_description":"Identity not found"}
Headers:
Content-Length: xx
Content-Type: xxxxx
Date: xxxxx
Server: xxxx/xxxxxxxxxx
At line:71 char:5
+ Connect-AzAccount -Identity;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Connect-AzAccount],CredentialUnavailableException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Profile.ConnectAzureRmAccountCommand
解决方法
据我所知,为了执行 catch
,您应该包含 -ErrorAction Stop
。
所以你的代码应该是:
Connect-AzAccount -Identity -ErrorAction Stop
您还可以设置:
$ErrorActionPreference = "Stop"
在您的 cmdlet 顶部。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。