As of today (June 28th, 2024), the Microsoft.Graph and PnP.PowerShell modules do not play well together in an Azure Automation Account runbook. I upgraded my PnP.PowerShell version to 2.4.0 two weeks ago, and was able to execute a Runbook that uses PnP.PowerShell and Graph without issue. Starting a few days ago, the same Runbook would not run and output the following errors:
ClientCertificateCredential authentication failed: Could not load type 'Microsoft.Identity.Client.BaseAbstractApplicationBuilder`1' from assembly 'Microsoft.Identity.Client, Version=4.50.0.0, Culture=neutral, PublicKeyToken=
Could not load type 'Microsoft.Graph.Authentication.AzureIdentityAccessTokenProvider' from assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=
The fix is simple.
Remove the PnP.PowerShell 2.4.0 module from your Automation Account.
From your desktop, open a Powershell terminal and enter the following:Install-Module -Name PnP.PowerShell -RequiredVersion 2.3.0
Save-Module -Name PnP.PowerShell -RequiredVersion 2.3.0 -Path c:\PATH
Zip the exported folder, creating a new file named PnP.PowerShell.zip
Upload the zip file to the Automation Account.
You can review or release of the PnP module here:
https://www.powershellgallery.com/packages/PnP.PowerShell/2.3.0
I went with 2.3.0 without issue, but Microsoft documentation notes that 2.1.1 is the module’s stable release. Noted here: https://pnp.github.io/powershell/articles/azureautomationrunbook.html#specific-stable-version
You should start looking at it if you are not already using the Runtime Environments feature in an Automation Account. Using the feature would allow for easier testing of module updates before updating the core module in the Automation Account. A simple strategy would be to create a Runtime Environment for testing and one for production/release. Then, from your runbook(s), you can plug into the testing runtime, test it out, then update to the production/relase env. https://learn.microsoft.com/en-us/azure/automation/runtime-environment-overview