In recent SharePoint case, we have a need to monitor SharePoint user policy changes. The first tasks is to report the current SharePoint user policies for all web applications. Here is the quick powershell to dump the information to a csv file.
# Load SharePoint.Powershell
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null) {
Write-Host $(Get-Date -format "dd_MM_yyyy_HH_mm_ss") '- Loading SharePoint Powershell Snapin'
Write-Host
Add-PSSnapin "Microsoft.SharePoint.Powershell"
}
# Output file in csv format in current folder
$reportFile = $myinvocation.mycommand.path.Replace($MyInvocation.MyCommand.name,"") + 'Policy_' + $(Get-Date -format "dd_MM_yyyy_HH_mm_ss") + '.csv';
# Add header to the output file
$line = "WebAppURL" + “,” + "DisplayName" + “,” + "IsSystemUser" + “,” + "PolicyRoleBindings" + “,” + "UserName"
Add-Content $reportFile $line
# Loop through all web applications and get policies
$webApps = Get-SPWebApplication
foreach($webApp in $webApps)
{
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null) {
Write-Host $(Get-Date -format "dd_MM_yyyy_HH_mm_ss") '- Loading SharePoint Powershell Snapin'
Write-Host
Add-PSSnapin "Microsoft.SharePoint.Powershell"
}
# Output file in csv format in current folder
$reportFile = $myinvocation.mycommand.path.Replace($MyInvocation.MyCommand.name,"") + 'Policy_' + $(Get-Date -format "dd_MM_yyyy_HH_mm_ss") + '.csv';
# Add header to the output file
$line = "WebAppURL" + “,” + "DisplayName" + “,” + "IsSystemUser" + “,” + "PolicyRoleBindings" + “,” + "UserName"
Add-Content $reportFile $line
# Loop through all web applications and get policies
$webApps = Get-SPWebApplication
foreach($webApp in $webApps)
{
# Get policy collection
$policyCollection = $webApp.Policies
$policyCollection = $webApp.Policies
# Read each policy and write to output file
foreach ($pl in $policyCollection) {
# PolicyRoleBinding is a collection and need to get all names and write to single column in format {role name, role name}
$pr = $pl.PolicyRoleBindings
$PolicyRoleBinding = "{"
foreach ($p in $pr) {
$PolicyRoleBinding += $p.Name
$PolicyRoleBinding += ","
}
#$PolicyRoleBinding = $PolicyRoleBinding.Substring(0,$PolicyRoleBinding.Length-1)
#$PolicyRoleBinding = $PolicyRoleBinding.trimend(",")
$PolicyRoleBinding = $PolicyRoleBinding -replace ".$"
$PolicyRoleBinding += "}"
# Write each role in to one line
$line = $webApp.Url + “,” + '"' + $pl.DisplayName + '"' + “,” + $pl.IsSystemUser + “,” + '"' + $PolicyRoleBinding + '"' + “,” + $pl.UserName
Add-Content $reportFile $line
}
}
The output csv file looks like as the following screenshot.
No comments:
Post a Comment