web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / My powershell script w...
Power Automate
Unanswered

My powershell script works locally but not with Power Automate

(0) ShareShare
ReportReport
Posted on by 150

Hi everyone !

 

I'm using a server from work to use a powershell script so i can use that script to change the template and other stuff of my site using power automate. The problem is that the script works when I execute it using on my PC (locally) but it doesn't work using power automate. (only one line doesn't work)

 

The problem is where the change theme comment is in the script. I'm guessing that the Connect-SPOService isn't executing. Can anyone help with that ?

 

$nom = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($args[0])) # get the variable from the power automate flow
$Username = "XXX"
$Password = "XXX"
$SiteCollection = "https://XXX.sharepoint.com/sites/test1/"
$SiteCollectionTwo = "https://XXX.sharepoint.com/sites/$nom"
$AdminSite = "https://XXX-admin.sharepoint.com"

[SecureString]$SecurePass = ConvertTo-SecureString $Password -AsPlainText -Force
[System.Management.Automation.PSCredential]$PSCredentials = New-Object System.Management.Automation.PSCredential($Username, $SecurePass) 

$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $Username, $(convertto-securestring $Password -AsPlainText -Force)


try {
 Install-Module PnP.PowerShell -Force
 Install-Module -Name Microsoft.Online.SharePoint.PowerShell -Force
 Connect-PnPOnline -Url $SiteCollection -Credentials $PSCredentials 
 if (-not (Get-PnPContext)) {
 Write-Host "Error connecting to SharePoint Online, unable to establish context" -foregroundcolor black -backgroundcolor Red
 return 
 }
 else {
 try { # copy and apply a template to another site
 Get-PnPSiteTemplate -out template.pnp -Handlers PageContents -IncludeAllClientSidePages -PersistBrandingFiles -Force
 Connect-PnPOnline -Url $SiteCollectionTwo -Credentials $PSCredentials
 Invoke-PnPSiteTemplate .\template.pnp
 Write-Host $nom
 }
 catch {
 Write-Host "Error copy template" -foregroundcolor black -backgroundcolor Red
 }

 try { # change theme
 Connect-SPOService -Url $AdminSite -Credential $cred
 Set-SPOWebTheme -Theme "ThemeAjup2" -Web $SiteCollectionTwo
 }
 catch {
 Write-Host "Error Theme colour" -foregroundcolor black -backgroundcolor Red
 return
 }

 try { # change color of the header / add a logo
 <#Connect-PnPOnline -Url https://XXX.sharepoint.com/sites/test1
 Get-PnPWebHeader -HeaderBackgroundImageUrl "https://XXX.sharepoint.com/sites/$nom/Style%20Library/__extendedHeaderBackgroundImage__getsitelogo.jpg"#>
 Connect-PnPOnline -Url $SiteCollectionTwo -Credentials $PSCredentials
 $Web = Get-PnPWeb
 $Web.HeaderEmphasis = "Strong" 
 $Web.HeaderLayout = "Extended"
 $Web.SiteLogoUrl = "https://XXX.sharepoint.com/sites/DataroomAjup/SiteAssets/__rectSitelogo__getsitelogo.png"
 $Web.Update()
 Invoke-PnPQuery
 }
 catch {
 Write-Host "Error HeaderBackgroundImage" -foregroundcolor black -backgroundcolor Red
 return
 }

 <#try { # add a background image
 Connect-PnPOnline -Url $SiteCollectionTwo -Credentials $PSCredentials
 $Web = Get-PnPWebHeader
 $Web.HeaderBackgroundImageUrl = "https://XXX.sharepoint.com/sites/DataroomAjup/SiteAssets/__extendedHeaderBackgroundImage__getsitelogo.jpg"
 $Web.Update()
 Invoke-PnPQuery
 }#>

 try { # delete links in the navigation bar
 Connect-PnPOnline -Url $SiteCollectionTwo -Credentials $PSCredentials
 $navigationNodeCollection = Get-PnPNavigationNode -Location QuickLaunch # QuickLaunch is the navigation bar for communication sites
 $navigationNode = $navigationNodeCollection | Where-Object { $_.Title -eq "Pages" }
 Remove-PnPNavigationNode -Identity $navigationNode.Id -Force
 $navigationNode = $navigationNodeCollection | Where-Object { $_.Title -eq "Contenu du site" }
 Remove-PnPNavigationNode -Identity $navigationNode.Id -Force
 }
 catch {
 Write-Host "Error Navigation bar" -foregroundcolor black -backgroundcolor Red
 }

 try { # enable a footer
 Connect-PnPOnline -Url $SiteCollectionTwo -Credentials $PSCredentials
 Set-PnPFooter -Enabled:$true
 }
 catch {
 Write-Host "Error Footer" -foregroundcolor black -backgroundcolor Red
 return
 }
 }
 }
catch {
 Write-Host "Error connecting to SharePoint Online: $_.Exception.Message" -foregroundcolor black -backgroundcolor Red
 return
} 

 

Thank you!

Categories:
I have the same question (0)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Leaderboard > Power Automate

#1
trice602 Profile Picture

trice602 378 Super User 2025 Season 2

#2
David_MA Profile Picture

David_MA 273 Super User 2025 Season 2

#3
Expiscornovus Profile Picture

Expiscornovus 135 Most Valuable Professional

Last 30 days Overall leaderboard