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!

Report
All responses (
Answers (