在我使用的 Azure PowerShell 脚本中Add-AzureAccount
将用户登录到 Azure。但是如何检测用户是否未成功完成登录以便我可以中止脚本?
另一种方法是使用 try 和 catch 块。
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)