我们目前正在寻求使用 Powershell 在 Azure 环境中运行一些自动化测试脚本,但似乎遇到了障碍。似乎没有办法使用 AzureDefaultCredentials,我们能找到的唯一选择是使用用户名和密码。
我们找到了以下资源(以及类似的资源) https://learn.microsoft.com/en-us/azure/automation/shared-resources/credentials?tabs=azure-powershell
但它们似乎都表明你
所以它看起来像:
$myCredential = Get-AutomationPSCredential -Name 'MyCredential'
$userName = $myCredential.UserName
$securePassword = $myCredential.Password
$password = $myCredential.GetNetworkCredential().Password
当我们在.net和python中执行相同操作时,我们将使用DefaultCredentials,因此在本地计算机上,您使用az登录登录,脚本将使用这些,并且在函数应用程序中的VM中,VM将从系统分配的身份获取其凭据。
所以我们所做的就是
from azure.identity import DefaultAzureCredential
credential = DefaultAzureCredential()
但是PowerShell中似乎没有这样的功能,我们不需要使用用户名和密码,或者我们只是找错地方了
在 PowerShell 中使用 AzureDefaultCredentials:
要使用 PowerShell 非交互方式登录 Azure,您可以使用托管身份验证,或使用 PowerShell 命令进行服务主体身份验证,如 MS 文档中所述。
请参阅使用 PowerShell 进入 Azure 的非交互式身份验证。
$appid="xxxxx"
$credential = Get-Credential -UserName $appid
Connect-AzAccount -ServicePrincipal -Credential $credential -Tenant "xxxx"
connect-AzAccount -identity
注意:上述两种身份验证方法的工作方式与默认 Azure 凭据方式相同。
您还可以使用Get-Credential
PowerShell 命令直接存储和检索用户名和密码。存储后,按照 @jdweng 的建议传递凭据以及
Connect-AzAccount -credential
参数。