Open Abhijeet1719 opened 2 years ago
Hi @Abhijeet1719
Have a look at https://github.com/PowershellScripts/SharePointOnline-ScriptSamples/tree/develop/Site%20Management/Site%20Policy/RemovePolicy
I wrote that one for you
It is still in the testing phase, so please let me know what you think and how it is working for you
Hi @Abhijeet1719
Have a look at https://github.com/PowershellScripts/SharePointOnline-ScriptSamples/tree/develop/Site%20Management/Site%20Policy/RemovePolicy
I wrote that one for you
It is still in the testing phase, so please let me know what you think and how it is working for you
@PowershellScripts Thanks for this..
but I am getting error with this script.
Please find attached screenshot.
That's good feedback. I'm guessing it comes from the fact that you published the content type from the content type hub. Let me try to simulate that and I will see what can be done.
@Abhijeet1719 What do you get when you try Get-PnPPolicy
on the site?
@PowershellScripts I tried below command but same error... Get-PnPSitePolicy -Name $PolicyName
@PowershellScripts Another strange thing is I am trying to run below script to apply policy using powershell but i am not able to apply it. I applied- removed it manually and then again I tried to apply using script its worked. So first you have to manually apply it then only its working. For new site collection where I didnt apply manually for that site collection below script is not working.
-------Script--------- Import-Module SharePointPnPPowerShellOnline
$filePath = $PSScriptRoot + "\SiteURL.csv" $CSVData = Import-Csv -LiteralPath $filePath
foreach ($row in $CSVData) { $SiteUrl = $row.Site
#Add-PnPStoredCredential -Name $SiteName -Username $cred.UserName -Password $cred.Password
$connection = Connect-PnPOnline -Url $SiteUrl -UseWebLogin
$PolicyName= "SPOSitePolicy"
#Call the function to apply site policy
Apply-PnPSitePolicy -SiteUrl $SiteUrl -PolicyName $PolicyName
}
Function Apply-PnPSitePolicy([String]$SiteUrl, [String]$PolicyName) {
try
{
Connect-PnPOnline -Url $SiteUrl -UseWebLogin
$ContentTypeID = "0x010085EC78BE64F9478AAE3ED069093B9963"
#Check if "Site Policy" Feature is active
$SitePolicyFeature = Get-PnPFeature -Identity "2fcd5f8a-26b7-4a6a-9755-918566dba90a" -Scope Site -Web $SiteUrl
If($SitePolicyFeature.DefinitionId -eq $null)
{
#Activate "Site Policy" Feature for the site collection
Enable-PnPFeature -Identity "2fcd5f8a-26b7-4a6a-9755-918566dba90a" -Scope Site
Write-Host "Site Policy Feature is Activated at $($SiteUrl)" -ForegroundColor Green
#Start-Sleep 30
}
#Get Policy to Activate
$SitePolicyToActivate = Get-PnPSitePolicy -Name "SPOSitePolicy"
# $SitePolicyToActivate = Get-PnPSitePolicy -AllAvailable
# $SitePolicyToActivate = Get-PnPContentType | where {($_.Id -match $ContentTypeID) -and ($_.Id -notlike $ContentTypeID)} | select name, id
foreach($sitePolicy in $SitePolicyToActivate)
{
Write-Host "Policy name-" $sitePolicy.Name
}
If ($SitePolicyToActivate)
{
#Remove content type
# Remove-PnPContentType -Identity $PolicyName -Force
#Apply Site Policy
Set-PnPSitePolicy -Name $SitePolicyToActivate.Name
#Close the site
Set-PnPSiteClosure -State Closed
Write-Host "Site Policy Applied to $($SiteUrl)" -ForegroundColor Green
}
Else
{
write-Host "Site Policy '$($PolicyName)' not found in Site $($SiteUrl)" -ForegroundColor Yellow
}
}
catch
{
$e = $_.Exception
$line = $_.InvocationInfo.ScriptLineNumber
$msg = $e.Message
Write-Host –ForegroundColor Red "Caught Exception: $e at $line"
Write-Host $msg
Write-Host "Something went wrong"
}
}
@PowershellScripts HI Do u have update for me?
Thanks....
@PowershellScripts I have created site policy in content type hub and trying to apply site policy to site collection using powershell but its not applying. I am able to activate at site collection level but not able to apply it. I tried it with another approach i.e applied it manually and then remove it and after that I tried with powershell it worked...bt I want to applied it without manually activating and de activating. Can anyone please suggest? Also, Our tenancy has multiple site collections and webs, and all have a Site Policy applied. I've now been asked to remove the policy from all sites. Doing it manually is not an option.
Is there a CSOM or PnP command or snippet I can execute via PowerShell that will allow me to remove a Site Policy from a site?