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:
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:
PnP SharePoint Online PowerShell 5.1 … https://github.com/pnp/PnP-PowerShell,
PnP.PowerShell PowerShell 7 https://github.com/pnp/powershell, and
PowerShell Pro Shell Tools… https://ironmansoftware.com/powershell-pro-tools/
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…
Right click on the PowerShellFormSiteScripting.PS1 file and click Run with PowerShell.
First thing you must do is authenticate. Provide a User Name, Password and your Tenant Domain Name:
Once the details are entered, click the
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.
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.
Previously, when the User click the arrows to either enable or disable scripting we processed the change by executing the code below:
Instead we can do this in a single line with PnP.PowerShell:
Set-PnpSite -Identity SiteUrl -NoScriptSite True/False -Connection ConnectionVariable