I'm trying to set up a system that automates gathering SharePoint Online statistics weekly. My plan is to use Azure Automation Accounts for this purpose, and I'm hoping to utilize a Service Principal that has the necessary access. This Service Principal has been configured with a certificate and password for authentication.
However, I'm running into issues with the PnP.PowerShell module. I need version 2.12.0 of PnP.PowerShell, as I've read that Azure Automation only supports older versions at the moment. I've uploaded this module and tried running my script in both PowerShell 7.2 and 7.4 environments.
Here's what I encounter when I attempt to test my runbook:
- If I include the line `Import-Module PnP.PowerShell`, I get an error saying that the module wasn't loaded because the file wasn't found.
- If I skip that line, I still get a command not found error for `Connect-PnPOnline`.
I'm on day three of troubleshooting and most documentation is outdated or not relevant to my setup. I would really appreciate any examples or guidance on how to correctly set up the runbook, especially regarding the authentication using the Service Principal certificate and password. Also, I'm not currently using the Managed Identity option, but I plan to in the future.
3 Answers
It sounds like you're encountering some classic Azure Automation setup hurdles. Make sure you've added the PnP.PowerShell module to your Azure Automation account's environment so it can recognize it. You can check out the Microsoft documentation on importing modules to Azure Automation for details. Once that’s done, try saving your connection parameters in a variable and referencing it when you run your commands. I'll find some examples that could help you out and post them shortly!
Running into issues like this is frustrating! Another route you might want to consider is using the Microsoft Graph API instead of PnP.PowerShell. It offers a robust way to interact with SharePoint Online and you might find it easier to authenticate and retrieve stats that way! Give it a thought!
Did you set up a new runtime environment and add the PnP module to that? It can be a bit tricky to find older module versions in Azure Automation’s interface. Check out [this resource](https://doitpshway.com/managing-azure-automation-runtime-environments-via-powershell) that I use for managing older module versions; it should come in handy!
Here’s a quick example of how you might structure your connection parameters:
```powershell
$site = "https://yourtenant.sharepoint.com/sites/yoursite"
$CertThumbprint = "YOUR_CERT_THUMBPRINT"
$TenantName = "yourtenant.onmicrosoft.com"
$ClientID = "YOUR_CLIENT_ID"
$pnpConnectionParams = @{
Url = $site;
Thumbprint = $CertThumbprint;
Tenant = $TenantName;
ClientID = $ClientID
}
$SPOControl = Connect-PnPOnline @pnpConnectionParams -ReturnConnection
Get-PnPSubWeb -Recurse -IncludeRootWeb -Connection $SPOControl
```
This way, you leverage the saved connection and avoid issues when executing commands in Azure Automation!