在 Powershell 中使用 AzureDefaultCredentials

问题描述 投票:0回答:1

我们目前正在寻求使用 Powershell 在 Azure 环境中运行一些自动化测试脚本,但似乎遇到了障碍。似乎没有办法使用 AzureDefaultCredentials,我们能找到的唯一选择是使用用户名和密码。

我们找到了以下资源(以及类似的资源) https://learn.microsoft.com/en-us/azure/automation/shared-resources/credentials?tabs=azure-powershell

但它们似乎都表明你

  1. 创建自动化用户
  2. 创建凭证
  3. 设置用户名和密码
  4. 关闭 MFA
  5. 在脚本中添加用户名和密码(可能通过机密,使其更安全)

所以它看起来像:

$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中似乎没有这样的功能,我们不需要使用用户名和密码,或者我们只是找错地方了

azure powershell
1个回答
0
投票

在 PowerShell 中使用 AzureDefaultCredentials:

要使用 PowerShell 非交互方式登录 Azure,您可以使用托管身份验证,或使用 PowerShell 命令进行服务主体身份验证,如 MS 文档中所述。

请参阅使用 PowerShell 进入 Azure 的非交互式身份验证。

  1. 服务负责人:
$appid="xxxxx" $credential = Get-Credential -UserName $appid Connect-AzAccount -ServicePrincipal -Credential $credential -Tenant "xxxx"

enter image description here

  1. 托管身份:
connect-AzAccount -identity

enter image description here

注意:上述两种身份验证方法的工作方式与默认 Azure 凭据方式相同。

您还可以使用

Get-Credential

 PowerShell 命令直接存储和检索用户名和密码。存储后,按照 @jdweng 的建议传递凭据以及 
Connect-AzAccount -credential
 参数。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.