2

I did see this q/a: 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. 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.

I'm really confused at this point because all I have done at this point in Azure is:

  1. Create a new Azure account
  2. Provision an Azure API Management instance through the UI (which btw, takes like 20 minutes)
  3. Try the above code to connect to Azure inside of a Powershell Azure Function locally.

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.

$PWord is my Azure password

$tenant is the first thing I saw when I opened Azure AD:

enter image description here

What's wrong with how I'm trying to connect to Azure through Powershell Core?

notacorn
  • 3,526
  • 4
  • 30
  • 60
  • To use "Service Principal authentication for script log in" follow the doc here: https://learn.microsoft.com/en-us/powershell/azure/create-azure-service-principal-azureps?view=azps-4.2.0 – David Browne - Microsoft Jun 12 '20 at 01:04
  • https://learn.microsoft.com/en-us/powershell/azure/create-azure-service-principal-azureps?view=azps-4.2.0#sign-in-using-a-service-principal is no different from the example 3 i linked in my question – notacorn Jun 12 '20 at 01:17
  • myemail@gmail.com is not a service principal. You've got the right mechanism, but the wrong identity. – David Browne - Microsoft Jun 12 '20 at 01:19

2 Answers2

4

Based on Example 3, it asks for entering your application ID for the username and service principal secret as the password.

So you need to create a service principal at first. And then use its application ID and client secret as the credential.

$User = "{application id}"
$PWord = ConvertTo-SecureString -String "{client secret}" -AsPlainText -Force
Allen Wu
  • 15,529
  • 1
  • 9
  • 20
  • oh that would make a lot of sense, as opposed to my azure credentials lol – notacorn Jun 12 '20 at 01:26
  • @notacorn If there is any further question, please feel free to post here. – Allen Wu Jun 12 '20 at 01:44
  • everything just feels so clunky - im used to creating these kinds of service accounts through any kind of UI but I have absolutely no idea where to run `New-AzADServicePrincipal` - I've tried the VS terminal, my Windows powershell – notacorn Jun 12 '20 at 05:04
  • and since azure wants to bill me for using their cloud console im not touching that with a ten foot pole – notacorn Jun 12 '20 at 05:05
  • 1
    @notacorn Just open Windows Powershell ISE and log in with your Azure account: `Connect-AzAccount -Tenant {tenant id}`. Then use `$sp = New-AzADServicePrincipal $UnsecureSecret = ConvertFrom-SecureString -SecureString $sp.Secret -AsPlainText` to get the new ServicePrincipal's appliction id and secret. After that, log in with your ServicePrincipal's credential. – Allen Wu Jun 12 '20 at 05:34
  • im getting the following error in ISE: `PS C:\Windows\system32> Connect-AzAccount Connect-AzAccount : The term 'Connect-AzAccount' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.` but i think i figured out how to do the same thing through the ui which ill add as an answer – notacorn Jun 12 '20 at 05:36
  • @notacorn It seems that you didn't install Az module. See https://learn.microsoft.com/en-us/powershell/azure/install-az-ps?view=azps-4.2.0#install-the-azure-powershell-module. – Allen Wu Jun 12 '20 at 05:42
  • @notacorn Yes. You can create it in Azure AD registration. It's unnecessary to create it via powershell. If my answer is helpful. You can accept as answer. Thank you. – Allen Wu Jun 12 '20 at 05:45
  • @MS-DOC need to improve a lot, this is the missing legend of https://learn.microsoft.com/en-us/powershell/azure/authenticate-azureps?view=azps-5.5.0 – user881703 Mar 02 '21 at 10:07
-1

I don't like Azure documentation. It gives off a very different vibe from GCP and feels much less beginner friendly.

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. You'll see:

  1. In Azure Portal, navigate to the App Registrations page in 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. Like normal service account naming conventions. I don't think the account type matters but I chose Multitenant. Redirect URL has nothing to do with service principals, and honestly makes it all the more confusing. I would never associate service accounts with any kind of redirect url, but here we are.

  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.

  4. Guess what, you have only created 1/3 of your service account. It doesn't even have a password yet. Within your created app registration, there's a Certificates & Secrets page. 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. 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. 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. Navigate to your Subscriptions page and click on your active subcription. For some reason IAM is here, so click on that. 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. 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.

notacorn
  • 3,526
  • 4
  • 30
  • 60
  • 1
    The key point of this issue is that you are using a wrong Azure credential instead of ServicePrincipal credential. How to create a ServicePrincipal and is another part. And I have indicated in my answer that you need to create a ServicePrincipal. The information I provide is the answer to this question. If my answer does not exist, when someone else who has the same question sees this post, he cannot use the answer you provided to solve the problem. So can you accept my answer to help more people? – Allen Wu Jun 17 '20 at 05:52
  • your answer is verbatim what was suggested directly as a comment 20 minutes before you answered - my answer just provides a step by step of how I solved my problem – notacorn Jun 17 '20 at 17:38
  • not sure why this is the accepted answer. – Anonymous Type Jan 04 '22 at 22:56