【问题标题】:Calling the connect cmdlets in a script在脚本中调用连接 cmdlet
【发布时间】:2019-06-25 16:32:32
【问题描述】:

我正在尝试编写一些 powershell 脚本,它必须结合 Az cmdlet 和 AzureRM 来完成我想做的一些事情。

当我在同一个脚本中调用 all 时,真正发生了什么: 连接-AzAccount 连接-AzureAD 连接-AzureRMAccount

最初,我调用 Get-Credential 并将其保存在一个变量中。 然后我使用这些凭据在 Connect-AzAccount 中填充 -Credential

然后由于多重身份验证,我必须调用 Connect-AzureAD,它会提示一个弹出窗口,允许用户从 MFA 文本到手机输入电子邮件、密码和代码。

在脚本的后面部分,RM 版本中有一些 cmdlet,因此我使用上面的先前凭据调用 Connect-AzureRMAccount。

$credentials = Get-Credential
$azureCredentials = New-Object System.Management.Automation.PSCredential ($credentials.UserName, $credentials.Password)   

Connect-AzAccount -Credential $azureCredentials -Tenant $tenantID -SubscriptionId $subscriptionID

Connect-AzureAD -Tenant $tenantID

Connect-AzureRMAccount -Credential $azureCredentials -Tenant $tenantID -SubscriptionId $subscriptionID 

在调用多个不同 Connect cmdlet 的整个脚本期间,在身份验证方面实际发生了什么。

由于某种原因,有一个特定的 cmdlet $AppRegistration = New-AzureADApplication -DisplayName $appName -HomePage $AppURI -IdentifierUris $AppURI -ReplyUrls $AppURI -PasswordCredentials $psadCredential

我在 Powershell 中收到一个错误,告诉我需要再次调用 Connect-AzureAD,即使它已经在脚本期间调用过一次。 MFA 会超时吗?

如何避免在运行脚本后让用户多次登录?

【问题讨论】:

  • 我的评论没有帮助。但我想说,我感受到了你的痛苦。 MFA 使 Azure 中的自动化成为一个巨大的痛苦。
  • 创建一个具有证书身份验证的服务主体,授予它对所需资源的权限,并使用它进行连接。这避免了用户必须登录。或者这应该与订阅无关?
  • 如果我的回复有帮助,请标记为答案(在我的回复左侧,有标记选项),谢谢。

标签: azure powershell visual-studio-code azure-active-directory


【解决方案1】:

我认为这不是一个正确的选项,如果你想避免 MFA,解决方法是创建一个服务主体(AD App),为其授予权限,然后你可以使用没有 MFA 的服务主体登录.

您可以按照以下步骤操作。

1.Create an Azure Active Directory application,然后是Upload a certificateGet values for signing in

2.导航到门户中的Azure Active Directory -> Roles and administrators -> 单击Application administrator -> Add assignment -> 按您的 AD 应用名称(服务主体名称)搜索 -> 选择它 -> @ 987654332@.

注意:在您的情况下,您想使用命令New-AzureADApplication,因此您需要将Application administrator 目录角色赋予您的AD App(服务主体),如果您想做其他事情需要更多权限,你可能需要给Global administrator这样的角色,这取决于你。

3.然后您可以使用下面的命令使用Az模块和AzureAD模块登录。

Connect-AzAccount -CertificateThumbprint "F1D9FE13A8FBxxxx1C8B07D1666" -ApplicationId "aa60b5df-xxxxxx8ae8e0cc2e4" -Tenant "bb58915cxxxxxxb97ed6c65" -ServicePrincipal

Connect-AzureAD -CertificateThumbprint "F1D9FE13A8FBxxxx1C8B07D1666" -ApplicationId "aa60b5df-xxxxxx8ae8e0cc2e4" -Tenant "bb58915cxxxxxxb97ed6c65" 

New-AzureADApplication -DisplayName "newapp"  -IdentifierUris "http://mynewapp11.contoso.com"

【讨论】:

    猜你喜欢
    • 2014-02-28
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 2021-07-12
    相关资源
    最近更新 更多