0

I am having an issue with importing PowerShell modules into a runbook in an automation account. It's a simple script to look at Exchange groups and email a list of matching groups out. It falls down at almost the first step: import-module exchangeonlinemanagement.

I installed the module in the normal way - from the gallery. Although it did import the module, the next command - connect-exchangeonline failed with "System.Management.Automation.RuntimeException: Unable to find type [Microsoft.Exchange.Management.RestApiClient.ExchangeEnvironment]."

Searching online, it suggested downgrading to version 3.5. I did that using the Azure Automation tab on https://www.powershellgallery.com/packages/ExchangeOnlineManagement/3.5.0

Now, when it reaches the import command it says that there is no module with that name. "The specified module 'ExchangeOnlineManagement' was not loaded because no valid module file was found in any module directory." Searching online doesn't provide any useful suggestions.

The commands work fine in the CLI.

Does anyone have any suggestions?

The automation account has the module installed

1
  • Usually problems like this are due the environmental variable PSMODULEPATH not being set properly. It may have to due to the account that was used to install the module. I would try to reinstall the module as an admin and make sure you install for all users. Downgrading is probably using the wrong folder for the module. Commented Jan 16 at 16:48

1 Answer 1

2

You need to create a new Runbook using Runtime 5.1.

Funny you should mention this, I just so happened to be researching this yesterday, only to come into work this morning to find a brand new search result! I am posting this less than 10 minutes after figuring out the fix.

Anyways, I did the exact same thing you did, except I troubleshot by using "Get-Module -ListAvailable" in a runtime 5.1 and a 7.2 runbook. The ExchangeOnlineManagement module did not show in 7.2 but did in 5.1.

After confirming the module was showing in the 5.1 runbook, I then tested the actual "Connect-ExchangeOnline" cmd and was able to see that it connected.

Sign up to request clarification or add additional context in comments.

4 Comments

That certainly worked for that error. Unfortunately it has introduced another. Did this version have a problem with get-azautomationcredential? It's telling me that access is denied, even though I have owner to the subscription and added the credentials to the automation account myself. Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) (Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))) I have tried using the 5.1 version of az-automation, but it hasn't made any difference.
I've resolved that: I realised that the managed identity didn't even have access to the automation account, so could not get the saved credentials. Now I am struggling to use connect-exchangeonline - it doesn't seem to like get-azautomationcredential. I have tried saving the get to a different variable and using that, and having the get credentials on the same line.
System.Management.Automation.ParameterBindingArgumentTransformationException: Cannot process argument transformation on parameter 'Credential'. A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: Enter your credentials. ---> System.Management.Automation.Host.HostException: A command that prompts the user failed because the host program or the command type does not support user interaction.
I managed to figure this out too. The connect-exchangeonline only works with managed identities now, when I changed it to using that instead of saved credentials it started workin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.