【问题标题】:Use the AZ module in a non-interactive environment?在非交互环境中使用 AZ 模块?
【发布时间】:2020-11-10 16:23:31
【问题描述】:

我希望能够使用 Az 模块从 Azure 密钥保管库中检索机密,以用于已部署到服务器并由 Windows 任务计划程序每天运行的 PowerShell 脚本。

最初,我需要遵循 oauth(猜测)过程:

Connect-AzAccount -Tenant '69a29f45-...'

重定向到https://login.microsoftonline.com/...,要求您选择一个帐户:

最终,它表示成功:

Authentication complete. You can return to the application. Feel free to close this browser tab.

完成此操作后,检索密钥的脚本将按预期工作:

...
$AccessToken = Get-AzKeyVaultSecret -VaultName 'MyVault' -Name 'MySecret' | Select-Object -ExpandProperty SecretValue | ConvertFrom-SecureString -AsPlainText
...

我担心令牌会过期,导致我的脚本失败。

SharePoint 模块 (Pnp.PowerShell) 可以make use of a credential stored in Windows Credential ManagerAz 模块也可以这样做吗?

如果没有,是否有其他方法可以在不交互的情况下处理此身份验证过程?

【问题讨论】:

  • 嗨,这个问题有什么更新吗?

标签: powershell azure-powershell powershell-core powershell-7.0


【解决方案1】:

您可以使用与 AD 租户中的服务主体 (SP) 绑定的证书登录。然后,您只需确保 SP 至少可以作为读取器访问您的密钥保管库。

【讨论】:

    【解决方案2】:

    看来我们无法将Az 模块与Windows 凭据管理器一起使用,要以非交互方式使用Az powershell,我们始终使用服务主体,请按照以下步骤操作。

    1.Register an application with Azure AD and create a service principal.

    2.Get values for signing increate a new application secret

    3.然后使用下面的命令。

    注意:不要忘记先将服务主体添加到Access policies,并在门户中使用密钥库的秘密权限。

    $azureAplicationId ="<application-id>"
    $azureTenantId= "<tenant-id>"
    $azurePassword = ConvertTo-SecureString "<client-secret>" -AsPlainText -Force
    $psCred = New-Object System.Management.Automation.PSCredential($azureAplicationId , $azurePassword)
    Connect-AzAccount -Credential $psCred -TenantId $azureTenantId -ServicePrincipal 
    #get the secret
    $AccessToken = Get-AzKeyVaultSecret -VaultName 'MyVault' -Name 'MySecret' | Select-Object -ExpandProperty SecretValue | ConvertFrom-SecureString -AsPlainText
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-16
      • 2016-02-17
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多