Creating Azure Runbook Jobs For SharePoint Online Using PnP PowerShell

Introduction
 
In this article, you will learn how to create an Azure Runbook job on Azure automation for SharePoint online portal, using PnP PowerShell cmdlets.
 
Steps involved
 
1. Create an automation account.
2. Import PnP PowerShell references as a module.
3. Add credentials.
4. Create Runbook
5. Add the code.
6. Test/Schedule.
7. View the job results.
 
In this article, I am not explaining more about the creation of an automation account and adding the credentials. I already explained about that in my previous articles. 
Importing PnP PowerShell Module
  1. From the automation account, click Assets -> Click Modules -> Click Browse gallery from the modules section -> Find for "SharePoint PnP PowerShell Online".

    assets

  2. Click the result. Click Import and click OK on the next pane.

    result

  3. Once imported, you can see the imported module under the modules section.

    modules  
Add SPO Credentials
 
For automation of the scripts, SharePoint online portal credentials needs to be stored on Azure portal. You can save the credentials in the credentials section under the assets component.
 
assets 
 
Create Runbook
 
Create a new Runbook or you can use the Runbook, if it's already available.
 
Runbook
 
Add Automation Script
  1. Click the created or existing Runbook. Afterwards, click edit button on the Runbook.

    edit  

  2. You can access or refer to the PnP cmdlet added to the Runbook, by expanding the cmdlets section and clicking on "SharePointPnPPowerShellOnline" cmdlet.

  3. In this example, we will see, how we can get all the lists available under the particular site collection. The code snippet, given below, shows retrieving the lists and displaying the title of the lists available. 
    1. function GetListsFromSite(){    
    2.     $creds = Get-AutomationPSCredential -Name "nakkeeranspo"    
    3.     Connect-SPOnline -Url "https://nakkeerann.sharepoint.com" -Credential $creds    
    4.     $list = Get-SPOList     
    5.     Write-Output $list.Title    
    6. }    
    7. GetListsFromSite   
  4. You can add the code to the script editor and save it. The snapshot, given below, shows the Runbook with the script added to the script editor.

    add the code 
Test the runbook
  1. Save the script and click the test pane.

  2. Click Start -> Test pane. The task will be queued and then the job will run after few seconds.

    The snapshot, given below, shows the results of the running test pane. It displays the lists available on the site.

    test  
Scheduling the Job
  1. From the edit section of Runbook, publish the Runbook.

    runbook 

  2. From the Runbook, click Schedules -> click "add a schedule" -> Link a schedule -> Create a new schedule -> Provide the details.

    scedhule

  3. You will see the listed schedules.

    schedules  

  4. Wait until the job runs. After the scheduled run time, click the jobs to see the results. Once the job is created during the run time, Job Status will change from the queued to running and then to completed.

    job  

  5. Once the job completes, click the completed job and then click on the output tile to view the results. 

    results 
Summary
 
Thus, you have learned, how to run PnP PowerShell scripts on Azure Runbook as the jobs for automation. For a basic understanding, we have seen, how the lists can be retrieved from the portal once. The complex scripts can be inserted and monitored, based on the requirements.