PowerShell Pro Tools Suite

Enable SharePoint Site Scripts with PnP PowerShell and a Windows Form

Updates: 25/01/2021

Script now includes support for PowerShell 7 and the latest release of PnP.PowerShell. The script detects if the modules are available and if not, installs them, which will require ‘Run as Administrator’.

Introduced ThreadJob to work-a-round an issue with PnP.PowerShell (7) when getting a connection to the tenant, it just hangs. Problem only occurred using credentials or -UseWebLogin from within a form. FYI. Start-Job deserialized the connection response so wasn’t usable.

A simple System.Windows.Forms.ListBox is used to display messages and errors.

Background

Occasionally with SharePoint, we need to enable Custom Scripts for a Site (Site Collection). In the old admin centre we were able to enable the option, as you can see in the image below:

Old admin centre - enable Custom Scripts

Old admin centre - enable Custom Scripts

SharePoint admins cannot be reliant on customising SharePoint through the UI and instead have to result to using PowerShell. Two tools I particularly like to use are:

The latter I like to use if I’m providing a client with a more friendly UI to interact with PowerShell, however, while the tool provides a mechanism to output an exe file, my preference is to give them something they can, should they wish, tweak when they become more familiar with SharePoint PS Admin.

Enable/Disable Custom Script

There are many articles on the internet that demonstrate how, using the command-line, admins can enable or disable custom scripting for Sites in SharePoint. This article summarises how a more user friendly user interface was created using a windows form over the top. A copy of the script I produced is here:

Overview of how it works

Much like many of the scripts online, it simply connects to the Tenant, retrieves all sites and organises them into two lists, those enabled and those disabled.

Having downloaded my copy of the script, you can either manually install the required scripts or, run the PowerShellFormSiteScripting.ps1 as an Administrator and it will install the required components. Unzip my script file where you will see there are three files…

Date modified are likely to be newer as it gets updated.

Date modified are likely to be newer as it gets updated.

Right click on the PowerShellFormSiteScripting.PS1 file and click Run with PowerShell.

Note: if using PowerShell 7 the option is slightly different and says ‘Run with PowerShell 7’

Note: if using PowerShell 7 the option is slightly different and says ‘Run with PowerShell 7’

First thing you must do is authenticate. Provide a User Name, Password and your Tenant Domain Name:

You only need to provide your tenant domain name not the complete URL.

You only need to provide your tenant domain name not the complete URL.

Once the details are entered, click the

Click the button. You can use  this button to refresh the lists at anytime.

Click the button. You can use this button to refresh the lists at anytime.

Assuming the details are entered the two lists will be populated with Site URLs where Custom Scripts is either enabled or disabled. If any of the information provided is incorrect then a warning will appear at the bottom of the form.

powershell_Site scripts enabled disabled.png

Enable or Disable Custom Script for Site

Simply select the Site you wish to Enable or Disable and click the arrow in the directional arrow. The video below demonstrates it working…

Under the hood

As i mentioned at the beginning there are many articles on this subject and the approach I take only really differs by the use of a Form.

First, it checks that the PnP modules are installed… if not the script will attempt to install them for you.

Next, when the user clicks Query SharePoint sites for Script Enablement, get all tenant sites, I chose to filter those out that had a different domain name e.g. My Sites.

Note: for PS 7 it’s using as ThreadJob to get the connection. This a work-a-round to an issue within using Connect-PnPOnline with Credentials within a Windows Form.

Note: for PS 7 it’s using as ThreadJob to get the connection. This a work-a-round to an issue within using Connect-PnPOnline with Credentials within a Windows Form.

Unlike PS 7 this is example is just doing the connection within the Form itself

Unlike PS 7 this is example is just doing the connection within the Form itself

Previously, when the User click the arrows to either enable or disable scripting we processed the change by executing the code below:

Screenshot+2020-09-13+at+12.35.42.jpg

Instead we can do this in a single line with PnP.PowerShell:

Set-PnpSite -Identity SiteUrl -NoScriptSite True/False -Connection ConnectionVariable