Microsoft 365 – SharePoint Online – PnP PowerShell – To install app on given SharePoint online sites

PowerShell script to install SharePoint app on given site collections using PnP CMDLETS
PowerShell script to install SharePoint app on given site collections using PnP CMDLETS

Hi All,

Greetings for the day!!!

Today new PowerShell – Installing App using PnP PowerShell on SharePoint Online site

Use Case / Background

  • In our tenant we have multiple modern communication site collections and subsites
  • We need to add google analytics script on our sites
  • So we have written App which installs scripts
  • We have uploaded our App in tenant app catalog
  • Now we need to install this App on the multiple site collections / subsites where we need it

Solution

  • So here we thought to have PowerShell script
  • Our script will iterate through CSV file having URLs of our SharePoint online sites on which we need to install the respective given APP

So we prepared the CSV file having column “URL” of sites where we need to install the APP as

PowerShell script

  • Initialize the required variables as
    • CSV file path
    • Success log file path – In this file we are maintaining logs / URLs of sites on which app is installed successfully
    • Error log file path – In this file we are maintaining logs / URLs of sites on which app is not installed successfully
    • App name – which we need to install – here we are installing app – “Image Map”
    • Credential file path – secrets.txt – we are reading credentials from file stored on local drive
    #CSV file path
    $csvFilePath = "C:\Prasham\Articles\PowerShell\sitecollections.csv"

    #success log file path
    $logFileName = "C:\Prasham\Articles\PowerShell\successlog.csv"

    #error log file path
    $errorlogFileName = "C:\Prasham\Articles\PowerShell\errorlog.csv"

    $AppName = "Image Map"

    #get the credentials - currently reading from text file - screts.txt
    #we are using Get-Content PowerShell CMDLET to read file
    #in credential file - secrets.txt - we are defining user name and password
    $getCreds = Get-Content -Path "C:\Prasham\Articles\PowerShell\secrets.txt"

Microsoft 365 - PowerShell - credential file - secret.txt - containing UN and PW
fig : Microsoft 365 – PowerShell – credential file – secret.txt – containing UN and PW

  • As we are reading credential file, we will have username and password as follows

$UserName = $getCreds[0]
$Password = $getCreds[1]

  • Next step is to create credential object as
$SecurePassword = ConvertTo-SecureString -String $Password -AsPlainText -Force
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName, $SecurePassword

#read the CSV file having URLs of all sites on which we need to hide the title

$sites = Import-CSV $csvFilePath

  • We will iterate through each site
foreach($site in $sites) {
        $url = $site

  • Here we are using PnP CMDLETS
  • We will connect to the site using – Connect-PnPOnline as
Connect-PnPOnline -Url $url.URL -Credentials $Cred

  • As we connected to our tenant, we have CMDLET – to get the app as
#Get the App from App Catalog using PnP CMDLET - Get-PnPApp
$App = Get-PnPApp -Scope Tenant | Where {$_.Title -eq $AppName}

  • As we have app, we will install the app as
#Install App to the Site using PnP CMDLET - Install-PnPApp
Install-PnPApp -Identity $App.Id

  • Maintain the success logs as
#generate success log
Write-Host "app deployed for Site collection $url successfully :)"
Add-Content $logFileName -Value " app for Site collection $url.URL successfully :)"

  • We will also maintain error logs as

catch{
#generate error log
Write-Host “app for Site collection $url failed :(” $($_.Exception.Message)
Add-Content $errorlogFileName -Value “app deployment for Site collection failed – $site $($_.Exception.Message)”
continue;
}

COMPLETE SCRIPT

<#
.SYNOPSIS
This script reads CSV file having column – URL

.DESCRIPTION
    This script reads CSV file having column - URL - and install the given APP for the site collection / subsite with given URL

.NOTES
    Author: Prasham Sabadra
    Version 1.0 - initial release
#>
#intialise variables

#CSV file path
$csvFilePath = "C:\Prasham\Articles\PowerShell\sitecollections.csv"


#success log file path
$logFileName = "C:\Prasham\Articles\PowerShell\successlog.csv"

#error log file path
$errorlogFileName = "C:\Prasham\Articles\PowerShell\errorlog.csv"

$AppName = "Image Map"

#get the credentials - currently reading from text file
$getCreds = Get-Content -Path "C:\Prasham\Articles\PowerShell\secrets.txt"

#getting user name and password from file
$UserName = $getCreds[0]
$Password = $getCreds[1]

#creating credential object
$SecurePassword = ConvertTo-SecureString -String $Password -AsPlainText -Force
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName, $SecurePassword


#read the CSV file having URLs of all sites on which we need to hide the title
$sites = Import-CSV $csvFilePath

#iterating to each site from CSV file
foreach($site in $sites) {
    $url = $site
    try{ 
            #connecting to our site
            Connect-PnPOnline -Url $url.URL -Credentials $Cred

            #Get the App from App Catalog
            $App = Get-PnPApp -Scope Tenant | Where {$_.Title -eq $AppName}

            #Install App to the Site
            Install-PnPApp -Identity $App.Id

            #generate success log
            Write-Host "app deployed for Site collection $url successfully :)"
            Add-Content $logFileName -Value " app for Site collection $url.URL successfully :)"
        }
        catch{
            #generate error log
            Write-Host "app for Site collection $url failed :("  $($_.Exception.Message)
            Add-Content $errorlogFileName -Value "app deployment for Site collection failed -  $site  $($_.Exception.Message)"
            continue;
        }
}#foreach($site in $sites)

fig : Microsoft 365 - PowerShell - To install app on given SharePoint online sites
fig : Microsoft 365 – PowerShell – To install app on given SharePoint online sites

Thanks for reading πŸ™‚ HAVE a FANTASTIC LEARNING πŸ™‚ LIFE IS BEAUTIFUL πŸ™‚

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: