Install Script Generalization

If you have tried to execute scripts more than once, you have noticed that those are not practical. You need to type your user name and password quite often. It takes lot of effort if you want to use same scripts in different environments. It would mean that you need to make huge search and replace job every time you update something.

Now we will make those script more generic and with little effort you can easily use scripts in different tenants.

Configuration

Configurations script

The first thing is to create a common PowerShell Script that will contain all configurations. This file will be used by all other scripts. Now we need to make small change to one file and we can reuse same files over and over again.

Add New PowerShell Script Item ‘Configurations.ps1’ to Scripts project.

#
# Configurations.ps1
#
$Global:TenantName = “tenant”
$Global:ContentFolder = Join-Path $PSScriptRoot -ChildPath “..\Project.SharePointApp1.Web” -Resolve

# Do not change these lines
$Global:TenantRootUrl = “https://” + $Global:TenantName + “.sharepoint.com”
$Global:TenantAdminUrl = “https://” + $Global:TenantName + “-admin.sharepoint.com”
$Global:Credential = Get-SPOStoredCredential -Name $Global:TenantName -Type PSCredential

It defines following common variables for us:

  • TenantName contains the name of the tenancy.
  • TenantRootUrl contains the url to SharePoint root site of the tenant. As Microsoft has deprecated public site, we only have one root site.
  • TenantAdminUrl contains the url to SharePoint admin site. This needs to be used in some of the scripts.
  • Credential contains the credentials we are using to connect to the tenancy so that we don’t have to type them over and over again.

Credentials

Open Windows Credential Manager. The easiest way to do this is to type word credential to Windows Search. It gives you two results. Select Manage Windows Credentials.

Add a generic credential. Type your credentials. Make sure that you will type your tenant name to internet or network address field, because the script is looking for that information.

Using configuration

Common Settings

Now we have created one common configuration file and we have saved our credentials to secure place. We can start using this new way in other scripts.

Here is example script that activates our custom JavaScript files.

We can modify it a little bit. After this modification, you don’t need to enter user name and password anymore. If you want to use same scripts between development, testing and production environments you need to just change TenantName variable and everything works between different environments.

#
# ActivateScripts.ps1
#
& .\Configurations.ps1
$siteUrl = $Global:TenantRootUrl + “/sites/app1”
Connect-SPOnline -Url $siteUrl -Credentials $Global:Credential

We need to use local siteUrl variable, because PowerShell won’t allow use to concatenate two variables while calling Connect-SPOnline.

It would be possible to use parentheses to combine two varibles together. Then it would be Connect-SPOnline -Url ($Global:TenantRootUrl + “/sites/app1”) -Credentials $Global:Credential. Both ways are acceptable.

Upload files

We also had hard coded source path for the files. Now we can make that relative to script location so we can copy scripts to new computer and we don’t need to expect that it has same folder structure as our own development server.

#
# UpdateFiles.ps1
#
& .\Configurations.ps1
$siteUrl = $Global:TenantRootUrl + “/sites/app1”
Connect-SPOnline -Url $siteUrl -Credentials $Global:Credential
Add-SPOFile -Path (Join-Path $Global:ContentFolder “\Scripts\jquery-2.2.0.min.js”) -Folder “SiteAssets/Scripts/jQuery”
Add-SPOFile -Path (Join-Path $Global:ContentFolder “\Scripts\Project.js”) -Folder “SiteAssets/Scripts”

Leave a Reply

Your email address will not be published. Required fields are marked *

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