简体   繁体   English

如何在 Powershell Core 中连接 AzAccount(无提示)?

[英]How to Connect-AzAccount in Powershell Core (without prompt)?

I did see this q/a: Connect-AzAccount without prompt我确实看到了这个问题: Connect-AzAccount without prompt

But when I tried the accepted answer, I get the following error:但是当我尝试接受的答案时,我收到以下错误:

[6/12/2020 12:36:20 AM] ERROR: Connect-AzAccount: Username + Password authentication is not supported in PowerShell Core. Please use device code authentication for interactive log in, or Service Principal authentication for script log in.

So I went to example 3 of the Connect-AzAccount documentation which specifies the "Service Principal" authentication method, so I mix the two because the suggested vanilla Get-Credential triggers another interactive session.因此,我转到了指定“服务主体”身份验证方法的 Connect-AzAccount 文档的示例 3 ,因此我将两者混合,因为建议的普通Get-Credential会触发另一个交互式 session。 So here's the script now:所以现在是脚本:

    $User = "myemail@gmail.com"
    $PWord = ConvertTo-SecureString -String "**********" -AsPlainText -Force
    $tenant = "f*********************************"
    $Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User,$PWord
    # $Credential = Get-Credential
    Connect-AzAccount -Credential $Credential -Tenant $tenant -ServicePrincipal

which brings my next error: [6/12/2020 12:45:45 AM] ERROR: Connect-AzAccount: AADSTS700016: Application with identifier 'myemail' was not found in the directory 'f*********************************'. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You may have sent your authentication request to the wrong tenant.这带来了我的下一个错误: [6/12/2020 12:45:45 AM] ERROR: Connect-AzAccount: AADSTS700016: Application with identifier 'myemail' was not found in the directory 'f*********************************'. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You may have sent your authentication request to the wrong tenant. [6/12/2020 12:45:45 AM] ERROR: Connect-AzAccount: AADSTS700016: Application with identifier 'myemail' was not found in the directory 'f*********************************'. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You may have sent your authentication request to the wrong tenant.

I'm really confused at this point because all I have done at this point in Azure is:在这一点上我真的很困惑,因为我在 Azure 中所做的一切是:

  1. Create a new Azure account创建一个新的 Azure 帐户
  2. Provision an Azure API Management instance through the UI (which btw, takes like 20 minutes)通过 UI 配置 Azure API 管理实例(顺便说一句,大约需要 20 分钟)
  3. Try the above code to connect to Azure inside of a Powershell Azure Function locally.尝试使用上面的代码在本地连接到 Powershell Azure Z86408593C34AF77FDD1ZF98B0 内部的 Azure。

I think something is wrong with the information I've provided or how I've configured something.我认为我提供的信息或我的配置方式有问题。

$User is the email I signed up to Azure with. $User是我注册到 Azure 的 email。

$PWord is my Azure password $PWord是我的 Azure 密码

$tenant is the first thing I saw when I opened Azure AD: $tenant是我打开 Azure AD 时看到的第一件事:

在此处输入图像描述

What's wrong with how I'm trying to connect to Azure through Powershell Core?我试图通过 Powershell 核心连接到 Azure 有什么问题?

Based on Example 3, it asks for entering your application ID for the username and service principal secret as the password.基于示例 3,它要求输入您的应用程序 ID 作为用户名和服务主体机密作为密码。

So you need to create a service principal at first.因此,您首先需要创建一个服务主体 And then use its application ID and client secret as the credential.然后使用其应用程序 ID 和客户端密码作为凭证。

$User = "{application id}"
$PWord = ConvertTo-SecureString -String "{client secret}" -AsPlainText -Force

I don't like Azure documentation.我不喜欢 Azure 文档。 It gives off a very different vibe from GCP and feels much less beginner friendly.它散发出与 GCP 截然不同的氛围,感觉对初学者不太友好。

With that said, they did have some kind of write-up that addresses my issue of creating a service principal and using it to authenticate.话虽如此,他们确实有一些文章解决了我创建服务主体并使用它进行身份验证的问题。

I actually ended up just finding a video (and I never do this) because I wanted to skip past all the technical jargon and just create the darn service principal.实际上,我最终只是找到了一个视频(我从不这样做),因为我想跳过所有技术术语,只创建该死的服务主体。

It's not even intuitive - it's like Microsoft could have added a button in AZ AD or IAM that said "Create Service Principal" but no , you have to go to a bunch of other pages that say nothing about service principals.它甚至不直观——就像微软可以在 AZ AD 或 IAM 中添加一个按钮,上面写着“创建服务主体”,但,你必须 go 到一堆其他页面,这些页面对服务主体只字未提。 You'll see:你会看到的:

  1. In Azure Portal, navigate to the App Registrations page in Azure Active Directory.在 Azure 门户中,导航到 Azure Active Directory 中的应用注册页面。 What an "app registration" has to do with a service principal, I couldn't tell you. “应用程序注册”与服务主体有什么关系,我无法告诉你。 I also couldn't tell you what a service principal is, but I'd imagine it has something to do with service accounts.我也无法告诉您服务主体是什么,但我想它与服务帐户有关。

  2. Make a New Registration and give it some sort of name to describe what the scope of this service principal will entail.进行新注册并为其命名以描述此服务主体的 scope 将需要什么。 Like normal service account naming conventions.就像正常的服务帐户命名约定一样。 I don't think the account type matters but I chose Multitenant .我认为帐户类型并不重要,但我选择了Multitenant Redirect URL has nothing to do with service principals, and honestly makes it all the more confusing.重定向 URL 与服务主体无关,老实说让事情变得更加混乱。 I would never associate service accounts with any kind of redirect url, but here we are.我永远不会将服务帐户与任何类型的重定向 url 相关联,但我们在这里。

  3. You're going to arrive at a page with Display Name (the name of the service principal you gave it in step 2), Application (client) ID (this is actually your service account username, which is imo non-intuitive), and Object ID (I have no idea what this is but I never needed to use it.您将到达一个页面,其中显示名称(您在步骤 2 中提供的服务主体的名称)、应用程序(客户端)ID (这实际上是您的服务帐户用户名,这是 imo 不直观的),以及Object ID (我不知道这是什么,但我从来不需要使用它。

  4. Guess what, you have only created 1/3 of your service account.猜猜看,您只创建了 1/3 的服务帐户。 It doesn't even have a password yet.它甚至还没有密码。 Within your created app registration, there's a Certificates & Secrets page.在您创建的应用注册中,有一个Certificates & Secrets页面。 On that page, you want to add a new client secret.在该页面上,您要添加新的客户端密码。 For my description I just put my service principal "display name".对于我的描述,我只是把我的服务主体“显示名称”。 I don't think that was necessary because this client secret is within the scope of the app registration, so even if I named it "poop" I could reasonably assume what it was for.我认为这没有必要,因为这个客户端密码在应用程序注册的 scope 中,所以即使我将其命名为“便便”,我也可以合理地假设它的用途。 Azure will generate a nuanced client secret and display it, but not warn you that this is the only time you will be able to see the key. Azure 将生成一个细微的客户端密码并显示它,但不会警告您这是您唯一能够看到密钥的时间。 Copy it.复制它。 This is, in normal people talk, your service principal password.在普通人的谈话中,这是您的服务主体密码。

  5. For the last step, you need to get out of dodge, I mean Azure AD.最后一步,你需要避开闪避,我的意思是 Azure AD。 Navigate to your Subscriptions page and click on your active subcription.导航到您的订阅页面,然后单击您的活动订阅。 For some reason IAM is here, so click on that.出于某种原因IAM在这里,所以点击它。 At this point, your service principal has a username and password, but no actual permissions - you have to configure that manually too.此时,您的服务主体有用户名和密码,但没有实际权限 - 您也必须手动配置。 Click Add -> Add Role Assignment .单击添加->添加角色分配 For role, you should do your research but if it's not serious Contributor is probably a safe bet.对于角色,您应该进行研究,但如果不认真,贡献者可能是一个安全的选择。 It has read/write but it doesn't supersede Owner .它具有读/写功能,但不会取代Owner Make sure you're assigning access to a service principal, and search for its display name.确保为服务主体分配访问权限,然后搜索其显示名称。 Save .保存

With all of that done, Connect-AzAccount finally worked.完成所有这些后, Connect-AzAccount终于奏效了。

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

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