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
- Account for Azure
- Download a PowerShell script https://github.com/Vintaurus/azureautomationforsps
- SharePoint Account with Password (need permission for provisioning site)
- SharePoint List with the fields «Title», «Status» (Single text), and «Site» (Link)
1. Create Azure Automation
- Call Azure Portal https://portal.azure.com
- Create a new resource
- Add «Automation»
- 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
- Select the existing automation
- Go to «Shared Resources» and select «Credentials»
- 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».
- Go to the page https://www.powershellgallery.com/packages/SharePointPnPPowerShellOnline
- Select «Azure Automation» and click «Deploy to Azure Automation»
- In Azure, select the existing automation account
- 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.
- Go to «Runbook», click «Import a runbook» and upload PowerShell script from https://github.com/Vintaurus/azureautomationforsps
- Add your URL for SharePoint in the script
- Click «Test pane» and select «Start» check if your PowerShell script runs without error.
- 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.
- Go to «Schedules» and select «Add a schedule»
- Click «Link a schedule to your runbook», click «Create a new schedule», fill data
- 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.