PowerShell Script to Report on Active Directory Group usage in SharePoint Groups

Table of Contents

It is a common requirement to see what Active Directory groups are used within SharePoint Groups, this script will report on this in an XML file across a web application you specify.

[void][reflection.assembly]::Loadwithpartialname("Microsoft.SharePoint") | out-null
[void][reflection.assembly]::Loadwithpartialname("Microsoft.Office.Server") | out-null

#returns the SPWebApplication at the specified URL
function get-spwebapplication ([String]$webAppUrl=$(throw 'Parameter -webUrl is missing!')) {
return [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($webAppUrl);
}

#returns the SPSite at the specified URL
function get-spsite ([String]$webUrl=$(throw 'Parameter -webUrl is missing!'))
{
return New-Object -TypeName "Microsoft.SharePoint.SPSite" -ArgumentList "$webUrl";
}

#returns the SPSite object from the specified URL
function get-spweb ([String]$webUrl=$(throw 'Parameter -webUrl is missing!'))
{
$site = New-Object -TypeName "Microsoft.SharePoint.SPSite" -ArgumentList "$webUrl";
return $site.OpenWeb();
}

#write-outputs average and max # members of a SharePoint Group
function output-ADgroupsInSPSite([System.Xml.XmlTextWriter] $writer, [Microsoft.SharePoint.SPSite] $site) {
$writer.WriteStartElement("Groups");
$writer.WriteAttributeString("Groups.Count", $site.RootWeb.SiteGroups.Count);
foreach($siteGroup in $site.RootWeb.SiteGroups)
{
$writer.WriteStartElement("Group");
$writer.WriteAttributeString("Name", $siteGroup.Name);
foreach($user in $siteGroup.Users)
{
if ($user.IsDomainGroup -eq $true){ $writer.WriteStartElement("ADGroup"); $writer.WriteAttributeString("Name", $user.Name); $writer.WriteEndElement(); }}
$writer.WriteEndElement();
}
$writer.WriteEndElement();
}

function get-info ([System.Xml.XmlTextWriter] $writer, [String]$webUrl=$(throw 'Parameter -webUrl is missing!')) {
$webapp = get-spwebapplication $webUrl
$writer.WriteStartElement("WebApp");
$writer.WriteAttributeString("Sites.Count", $webapp.Sites.Count);
$writer.WriteAttributeString("Url", $webUrl);
foreach ($site in $webApp.Sites) { $writer.WriteStartElement("Site"); $writer.WriteAttributeString("AllWebs.Count", $site.AllWebs.Count); $writer.WriteAttributeString("Url", $site.Url); output-ADgroupsInSPSite $writer $site; $writer.WriteEndElement(); }
$writer.WriteEndElement();
}

$encoding = [System.Text.Encoding]::UTF8;
$writer = New-Object System.Xml.XmlTextWriter("c:\report.xml", $encoding );
$writer.Formatting = [system.xml.formatting]::indented;
$writer.WriteStartDocument();
$writer.WriteStartElement("Farm");
get-info $writer "http://site/"
$writer.WriteEndElement();
$writer.close()
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.



Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. Hosted generously by CustomWare