Link to home
Start Free TrialLog in
Avatar of iamuser
iamuser

asked on

How to run powershell scripts in Sharepoint Online

How do I run a Powershell command/script that calls a javascript file (stored in a folder within the main site). The powershell command/script should only apply to 1 specific site collection.

in particular, I found this script online but I can't make out how to run it:

Write-Host "Upload RemoveGearIcon.js"
 
$f = Add-PnPFile -Path .\RemoveGearIcon.js -Folder "scripts"
 
$site = Get-PnPSite
$fileUrl = $site.Url + "/Scripts/RemoveGearIcon.js"
 
Write-Host "Add JavaScript Link"
 
$jsLink = Get-PnPJavaScriptLink -Name "RemoveGearIcon" -Scope Site
 
if ($jsLink -ne $null)
{
    Remove-PnPJavaScriptLink -Identity "RemoveGearIcon" -Scope Site -Force
}
 
$link = Add-PnPJavaScriptLink -Name "RemoveGearIcon" -Url $fileUrl -Sequence 200 -Scope Site

Open in new window

Avatar of Daryl Sirota
Daryl Sirota
Flag of United States of America image

You can call any web tool with an "Invoke-WebRequest", but since you'll have to authenticate, it's a little harder.  Although, once the Js is called from SPO, what do you want to do with its result that is returned to the client?  What's your penultimate goal?  You might find the CSOM (Client Side Object Model) easier to manipulate SPO objects.

$user = "myusername"
$pass = "super-strong-alpha-numeric-symbolic-long-password"
$pair = "${user}:${pass}"
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($bytes)
$basicAuthValue = "Basic $base64"
$headers = @{ Authorization = $basicAuthValue }

Invoke-WebRequest -uri "https://site.sharepoint.com/site/library/bla.js" -Headers $headers
Avatar of iamuser
iamuser

ASKER

The powershell command was supposed to add a javascript link which it would call another script (within the site library) that would perform a specific action based on the user rights in sharepoint.
Sorry for the delayed response... (life intrudes!)  In any case, the powershell command is intended to ADD a link or just CALL a script?  Adding a new item via PowerShell is harder than calling.
Avatar of iamuser

ASKER

it was supposed to call a script. I have the script uploaded to a folder called "scripts".  I think the way it was supposed to work was that this, the powershell command would add a javascript link which would load and then call and execute a script.

I have a script that will hide the gear icon if you do not have full management rights. It works fine when i add this to a webpart but this does not carry over to other pages or to the document library. In order for it to apply to everything I have to apply the script throughout the site. So the way I understand this was to have powershell create a javascript link which is then applied to the site. So each time any page is loaded, the javascript link will call the script and check if the user has full management rights, if no, then the gear icon is hidden.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.