Skip to content

Get CSV Report Including Items Count, Broken Permissions, and Site Content in SharePoint 2007 Using PowerShell

by on June 2, 2014

This lets you use PowerShell and bypass stsadm to pull the information needed. Modified from blog post cited below for 2007.

SOLUTION .PS1 Script

<#
http://blog.kuppens-switsers.net/sharepoint/get-sites-webs-lists-and-number-of-items-with-their-permission-inheritance-in-sharepoint/
.SYNOPSIS
   Return all sites, webs and lists in a webapplication with the permission inheritance and the number of items with unique permissions.

.DESCRIPTION
   Return all sites, webs and lists in a webapplication with the permission inheritance and the number of items with unique permissions

.NOTES
   File Name: Get-ItemsAndPermissions.ps1
   Author   : Bart Kuppens
   Version  : 1.0

.PARAMETER WebApplication
   Specifies the URL of the Web Application.

.PARAMETER FieldDelimiter
   Specifies the delimiter which is used between the fields.

.EXAMPLE
   PS > .\Get-ItemsAndPermissions.ps1 -WebApplication http://sharepoint.mycompany.com -FieldDelimiter ";" > c:\ps_scripts\sitesReport.csv
#>
[CmdletBinding()]
param(
   [Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$false,HelpMessage="Specifies the URL of the Web Application.")]
   [string]$WebApplication,
   [Parameter(Position=1,Mandatory=$true,ValueFromPipeline=$false,HelpMessage="Specifies the delimiter which is used between the fields.")]
   [string]$FieldDelimiter
)

# Load the SharePoint PowerShell snapin if needed
#if ((Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA SilentlyContinue) -eq $null)
#{
#   Write-Host "Loading the SharePoint PowerShell snapin…"
#   Add-PSSnapin Microsoft.SharePoint.Powershell
#}

#for 2007 without powershell
#Load SharePoint Asssemblies
[System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[System.Reflection.Assembly]::Load(“Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
[System.Reflection.Assembly]::Load(“Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)

#sp2010
#$webapp = Get-SPWebApplication $WebApplication -ea silentlyContinue
#sp2007
$webApp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($WebApplication)

if ($webapp -eq $null)
{
   Write-Host -ForegroundColor Red "The URL $WebApplication is not a valid webapplication"
   exit
}
$sites = $webApp.Sites
$header = [string]::Format("Site{0}Web{0}List{0}# Items{0}Break Permission Inheritance{0}# Items with unique permissions", $FieldDelimiter)

Write-Output $header
foreach ($site in $sites)
{
   $siteUrl = $site.Url
   $webs = $site.AllWebs
   $detail = [string]::Format("$siteUrl{0}{0}{0}{0}{0}", $FieldDelimiter)
   Write-Output $detail
   foreach ($web in $webs)
   {
      $webUrl = $web.Url
      $lists = $web.Lists
      $webBreakInheritance = $web.HasUniqueRoleAssignments
      $detail = [string]::Format("$siteUrl{0}$webUrl{0}{0}{0}$webBreakInheritance{0}", $FieldDelimiter)
      Write-Output $detail
      foreach ($list in $lists)
      {
         $items = $list.Items
         $listItemCount = $items.Count
         $listBreakInheritance = $list.HasUniqueRoleAssignments
         $i = 0
         try
         {
            foreach ($item in $items)
            {
               if ($item.HasUniqueRoleAssignments)
               {
                  $i++
               }
            }
         }
         catch {}
         $detail = [string]::Format("$siteUrl{0}$webUrl{0}$($list.Title){0}$listItemCount{0}$listBreakInheritance{0}$i", $FieldDelimiter)
         Write-Output $detail
      }
      $web.Dispose()
   }
   $site.Dispose()
}

Advertisements
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: