繁体   English   中英

如何使用已注册应用程序的服务主体通过 PowerShell 模块连接到 Azure?

[英]How do I connect to Azure through PowerShell Modules, using the Service principal of a Registered App?

我需要创建一个 powershell 脚本来查询 Azure 资源。 我拥有的是应用程序注册。

应用程序注册为我们提供了以下信息:


    # --- APP REGISTRATION OUTPUT
    # appId = "***** APP ID *******"
    # displayName = "**** APP Name **** "
    # password = "***** SECRET *******"
    # tenant = "**** TENANT ID *****"

我现在需要使用这些凭据通过 PowerShell 脚本访问 Azure。

我尝试了以下方法:

$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $ApplicationId, $SecuredPassword
Connect-AzAccount -ServicePrincipal -TenantId $TenantId -Credential $Credential

但我得到一个错误:

 … Account -ServicePrincipal -TenantId $TenantId -Credential $Credential
     |  ~~~~~~~~~~~
     | Cannot bind argument to parameter 'Credential' because it is null.

我不认为我的所作所为是不正常的。 应用程序注册使我们能够允许应用程序(PowerShell 应用程序)与给定的租户进行交互? 还是我弄错了?

我不希望应用程序每次都使用帐户登录(即在脚本运行时打开浏览器 window)。

我究竟做错了什么?

您错过了将密码转换为安全字符串的机会。 您可以在$credential变量中验证这一点。

$ApplicationId = "0000-0000-0000-0000"
$Password = "000000000000000"
$TenantId = "0000-0000-000-000"
$subscriptionId = "0000-0000-0000-0000"
$SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $ApplicationId, $SecuredPassword
Connect-AzAccount -ServicePrincipal -TenantId $TenantId -Credential $Credential
$sub = Get-AzSubscription -SubscriptionId $subscriptionId
Set-AzContext -Subscription $sub

我已经在我的环境中复制,下面的脚本对我有用:

$appId ="53f3ed85-70c1c2d4aeac"   
$pswd="55z8Q~_N9SRajza8R"  
$t = "72f988bf-cd011db47"
[ValidateNotNullOrEmpty()]$pswd="55z8BU4oik.kVrZWyaK8R" $sp = ConvertTo-SecureString -String $pswd -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $appId, $sp
Connect-AzAccount -ServicePrincipal -TenantId $t -Credential $Credential

Output:

在此处输入图像描述

您需要像上面那样将秘密值(密码)转换为安全密码,然后它将像我的工作一样工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM