Blog

Provisioning SharePoint Site with Azure Automation and PnP-PowerShell

For some projects, we used with the scheduled task and PnP-PowerShell for site provisioning (like TimerJob, but we won’t use farm solution) on SharePoint On-Premises. Now I would like to check if it is easy to migrate from SharePoint On-Premises to SharePoint Online without significant change for the existing PowerShell scripts.

The solution is Azure Automation because it can be added to the module «SharePointPnPPowerShellOnline» and support PowerShell-script. This Azure Automation is an excellent condition without significant effort for migration.

Preparation

  1. Account for Azure
  2. Download a PowerShell script https://github.com/Vintaurus/azureautomationforsps
  3. SharePoint Account with Password (need permission for provisioning site)
  4. SharePoint List with the fields «Title», «Status» (Single text), and «Site» (Link)

1. Create Azure Automation

  1. Call Azure Portal https://portal.azure.com 
  2. Create a new resource
  3. Add «Automation»
  4. Fill data for Azure Automation

Now we have Azure Automation. Next step is to add a credential for SharePoint account

2. Add credentials for Azure Automation

  1. Select the existing automation
  2. Go to «Shared Resources» and select «Credentials»
  3. Click «Add  a credential» and enter data «SpsAdmin» and SharePoint Account

3. Import a PnP PowerShell Modul

Before the PowerShell-script is added to a runbook, the runbook needs a module «SharePointPnPPowerShellOnline».

  1. Go to the page https://www.powershellgallery.com/packages/SharePointPnPPowerShellOnline
  2. Select «Azure Automation» and click «Deploy to Azure Automation»
  3. In Azure, select the existing automation account
  4. Click «OK»

Now the module «SharePointPnPPowerShellOnline» is available for the runbook. You can check it in «Modules»:

4. Add item in the SharePoint List

Create a new item with the value of the title. The next step the runbook will run and create the site.

5. Create a runbook

Now we can add the existing PowerShell script to the runbook. The good news is here; we don’t change anything in the script. I created this script for the SharePoint on-premises and added it directly to a runbook in Azure.

  1. Go to «Runbook», click «Import a runbook» and upload PowerShell script from https://github.com/Vintaurus/azureautomationforsps
  2. Add your URL for SharePoint in the script
  3. Click «Test pane» and select «Start» check if your PowerShell script runs without error.
  4. Back to the script, click «Edit PowerShell Runbook» and select «Publish»

6. Create schedules

I want that the runbook should run every 5 minutes. So I have to create 12 schedules because the schedule can run minimal every 1 hour.

  1. Go to «Schedules» and select «Add a schedule»
  2. Click «Link a schedule to your runbook», click «Create a new schedule», fill data
  3. Now repeat for 9:05, 9:10, 9:15,…..9:55. After that, now all schedules should be linked to the runbook. 

Conclusion

Wow, the PnP-PowerShell-Script is added without change the code. It can migrate 1:1 from OnPremises to Azure.