gpt4 book ai didi

powershell - 如何检测Azure Add-AzureAccount登录是否失败或被取消?

转载 作者:行者123 更新时间:2023-12-01 11:34:56 24 4
gpt4 key购买 nike

在 Azure PowerShell 脚本中,我使用 Add-AzureAccount 将用户登录到 Azure。但是如何检测用户是否未成功完成登录以便我可以中止脚本?

最佳答案

执行此操作的另一种方法是使用 try 和 catch block 。

try {
Add-AzureAccount -ErrorAction Stop
}

catch {
Write-Error $_.Exception.Message
}

#Write the remaining script here
#Control won't come here unless the Add-AzureAccount was successful

Write-Verbose 'User logged in'

但是,任何 Microsoft 帐户都可以登录,即使它们没有任何关联的订阅。所以,这里有一些修改过的代码。

try {
$a = Add-AzureAccount -ErrorAction Stop
if ($a.Subscriptions) {
Write-Verbose 'User Logged in'
} else {
throw 'User logged in with no subscriptions'
}
}

catch {
Write-Error $_.Exception.Message
}

#Write the remaining script here
#Control won't come here unless the Add-AzureAccount was successful

关于powershell - 如何检测Azure Add-AzureAccount登录是否失败或被取消?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28105128/

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