question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using AzureAD module requires an awkward workaround

See original GitHub issue

I’ve created a powershell function app on a consumption plan and I’m trying to use the AzureAD module in my project. I’m getting this error

Exception: The current processor architecture is: X86. The module 'D:\home\site\wwwroot\Modules\AzureAD\2.0.2.16\AzureAD.psd1' requires the following architecture: Amd64.

I’ve set the platform settings to 64 bit, restarted the app, and ran the following commands in a http trigger

[Environment]::Is64BitProcess
[Environment]::Is64BitOperatingSystem

and got the following output

2019-05-23T20:18:23.336 [Information] OUTPUT: False
2019-05-23T20:18:23.337 [Information] OUTPUT: True

Is there any reason why setting the platform settings to 64 bit would not convert the platform to using a 64 bit process?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:34 (15 by maintainers)

github_iconTop GitHub Comments

4reactions
JeretSBcommented, Jul 15, 2020

For anyone else who may stumble on this. (Until they’ve finished correcting this.) The following finally got things working for me.

  1. Setting the application to run as x64 bit: Function App> Configuration > General Settings > Platform > 64 Bit
  2. Setting the app to run on Powershell 7 instead of 6 on this thread
  3. Used: Import-Module AzureAD -UseWindowsPowerShell

Thanks all for the feedback. Took me a couple hours to get this working. 😔

Big thanks @AnatoliB!

EDITED: Import-Function replaced with Import-Module, as this was a typo.

4reactions
mdrakiburrahmancommented, Jun 9, 2019

@abigailbuckholdt I was able to connect to AzureAD via a Powershell Azure Function to get a list of users back. It wasn’t easy, but here are the steps:

As @maertendMSFT mentioned above, there is a secret “preview” AzureAD module available in Azure Cloud Shell today, that’s supported on x86 (since Azure Shell uses PowerShell Core). There’s no way to get the x64 version of AzureAD working on Azure Function, unless Microsoft decides to support that architecture in Azure Function. Thus, we’re going to be waiting until this secret preview module is available for use in Azure Function etc, which obviously will take a lot of time. I decided to get this module out of the Cloud Shell, and import it as a standard module in my function, which is supported today in Azure Function (provided the module works on PowerShell Core of course)

Step 1: Getting that Preview PowerShell module out of Cloud Shell To do this, I first initiated a Cloud Shell instance, which in turn created a File Share. By running the two powershell commands in the screenshot below, you’re able to copy the entire Module Folder from the machine hosting Cloud Shell, and see it in the File Share. 1

Then, I used Storage Explorer to download it to my local: 2

Which looked like this: 3

Step 2: Getting the module uploaded to Azure Function, and getting it to work This part was really, really difficult. The thing is, I’m trying to use a Service Account to automate the task of Connect to Azure AD. But, Connect-AzureAD does not support connecting to a Service Account by using the ClientID and AppKey. You need to use a self-signed Cert and pass in the thumbprint.

This works fine and dandy on my local, but this cert based authentication feature is not supported in this Preview AzureAD (you can try it on Azure Cloud Shell, it’s going to return a .NETCORE error): 7

So - we now have the preview module that’ll work on Azure Function…but it doesn’t support Cert based authentication with service principals (i.e. it’s useless, since the only reason I’m building the function is to automate stuff)

So, I did a lot of digging around, and am basically doing the following to get this to work:

  1. First, using Visual Studio Code, create a “Modules” folder in the root of your function directory and paste the Preview Module in: 4

When you deploy to the function with VSCode, this whole folder will get uploaded to the machine hosting the Azure Function.

  1. To get around the limitation with the cert based authentication limitation I mentioned, what I did in run.ps1 was:
  • Sign into Az module (which is available OOTB in the Azure Function)
  • Extract the context token from the machine’s cache
  • Pass the token into Connect-AzureAD, at which point I’m able to connect to my Azure AD tenant and get a list of users

Here are all the steps in a screenshot: 5

Now when I use a REST Client (I use Insomnia) to make a call to the function, I’m getting the first user in my Tenant back, via the AzureAD module: 8

Hope this helps! Judging by all the technical hurdles Microsoft is going to have to have to overcome (specially supporting the Cert based authentication on an unsupported .NET Core platform), I’m willing to bet it’ll be some time before we see this AzureAD module functionality supported out of the box. None of this would be an issue if instead of Powershell Core, the regular 64-bit version was supported in Azure Function though…

Read more comments on GitHub >

github_iconTop Results From Across the Web

'Connect-AzureAD' is not recognized as a name of a cmdlet
The error message you're encountering is indicating that the AzureAD module is missing a required assembly, System.Windows.Forms, which is ...
Read more >
How to fix The term 'Connect-AzureAD' is not recognized
1. Start by running PowerShell as an administrator. · 2. Once PowerShell has opened, use the Install-Module AzureAD cmdlets with the -force parameter....
Read more >
Unable to install Azure Active Directory Module for ...
Method 1: Install Azure Active Directory Module for Windows PowerShell when you log on as local admin · Log on as a local...
Read more >
O365 refresh token. Copy the 'Application (client) ID and paste it
One is using Azure AD V2 authentication endpoint. The app will request a new login from the user. Microsoft 365: Domain or user...
Read more >
The term 'Connect-AzureAD' is not recognized as the name ...
Open PowerShell as an administrator, Enter: Install-Module AzureAD · Confirm the installation by entering “A” – Yes to All.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found