Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PowerShell script to check if a site policy exists in a specified list of SharePoint sites
<#
.SYNOPSIS
Tht script checks is a site policy exists in a site collection.
.DESCRIPTION
.PARAMETER SiteUrl
URL of a site collection to check the policy existence.
.PARAMETER PolicyName
Name of the policy to check.
#>
param(
# URL of a site collection to check the policy existence in.
[Uri]
$SiteUrl,
# URL of a web application where all the site collections will be checked for the policy existence.
[Uri]
$WebAppUrl,
# CSV file with the URLs of the site collections which will be checked for the policy existence.
[string]
$CsvFile,
# Name of the policy to check.
[string]
$PolicyName
)
Start-Transcript "$PSScriptRoot\Get-SitePolicy_$(Get-Date -Format "yyyyMMdd_HHmmss").txt"
Add-PSSnapin Microsoft.SharePoint.PowerShell;
$sw = [System.Diagnostics.Stopwatch]::StartNew();
<# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
MAIN
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #>
$siteUrls = Import-Csv $CsvFile
foreach($_ in $siteUrls) {
#$siteUrls % {
write-host "Opening site '$($_.Url)'... " -NoNewline
try {
$site = Get-SPSite $_.Url
Write-Host "DONE" -ForegroundColor Green
}
catch {
Write-Host "FAILED" -ForegroundColor Red
}
$policyFound = $false;
write-host "Reading policy '$PolicyName' in '$($_.Url)'..." -NoNewline
try {
$policies = [Microsoft.Office.RecordsManagement.InformationPolicy.ProjectPolicy]::GetProjectPolicies($site.RootWeb)
foreach($policy in $policies)
{
if($policy.Name -eq $PolicyName)
{
$policyFound = $true;
break;
}
}
if($policyFound)
{
Write-Host "FOUND" -ForegroundColor Green
}
else {
Write-Host "NOT FOUND" -ForegroundColor Red
}
}
catch {
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-Host "FAILED" -ForegroundColor Red
Write-Host "$FailedItem : $ErrorMessage"
}
$site.Dispose();
Write-Host
}
Write-Host "Total execution time: $($sw.Elapsed)"
Stop-Transcript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.