Roland Serman’s Blog

Just another WordPress.com weblog

List Site Owners for All Site Collections

Here’s a Powershell script I’ve managed to piece together that will output all Site Collection Owners (Not Site Collection Administrators) into either a csv or xml file.  In our SharePoint deployment we’re more of a service provider, hosting multiple web applications each containing one to many site collections, and we were looking for an easy way to determine who the site owners were for every site so that we could send notification prior to maintenance windows etc.  I’ve been working on this in my spare time over the past few weeks, feel free to leave any suggestions, I know it’s a bit convoluted.

#Generates a list of all site collections across all Web Applications,
#and outputing them into an array

$sitepath = New-Object System.Collections.ArrayList
$output=stsadm -o enumzoneurls
$x=[xml]$output
$x.ZoneUrls.Collection|
foreach-object -process {$y=stsadm -o enumsites -url $_.Default;$sites=[xml]$y;$sites.Sites.Site}|
foreach-object -Process {$sitepath=$sitepath + $_.Url}
$sitepath | Write-Output

#Define Output file, and labels

$filename = AllSiteUsers.csv
Write-Output “Site Collection,Title,Group,UserID,Email Address” | Out-File $filename -Append

#Loops through all site collections generating a list of Site Owners

[System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)
foreach ($object in $sitepath) {
$site=new-object Microsoft.SharePoint.SPSite($object)
$web=$site.Rootweb
$groups = $web.groups | ? {$_.Name -match “^.*Owners” }
foreach($group in $groups)
{
foreach($user in $group.users)
{
Write-Output “$($object),$web,$($group.Name),$user,$($user.Email)” | Out-File $filename -Append
$user
}
}}

#Converts the csv ouput to XML

Import-Csv AllSiteUsers.csv | Export-Clixml AllSiteUsers.xml

About these ads

20 responses to “List Site Owners for All Site Collections

  1. Mike January 29, 2009 at 4:59 pm

    Tried running this and got an error, how do you run this via command line

  2. Mike January 29, 2009 at 6:17 pm

    Not too familiar with powershell tried this:

    E:\Software\Powershell>powershell getsiteowners.ps1
    The term ‘getsiteowners.ps1′ is not recognized as a cmdlet, function, operable
    program, or script file. Verify the term and try again.
    At line:1 char:17
    + getsiteowners.ps1 <<<<

  3. rolandserman January 29, 2009 at 6:21 pm

    all you should have to do, is open powershell, browse to the folder the script is located in, just like you would from a command prompt. then run type the following:

    ./getsiteowners.ps1 (that is dot forward slash, then name of the script)

    If you get an error about execution policy, then you need to run the following:

    set-executionpolicy unrestricted

    And then run the script again.

  4. Mike January 29, 2009 at 6:29 pm

    Now im getting the following:

    PS E:\Software\Powershell> .\getsiteowners.ps1
    Cannot convert value “System.Object[]” to type “System.Xml.XmlDocument”. Error: “Unexpected end of file has occurred. The following elements are not closed: Sites. Line 3, position 84.”
    At E:\Software\Powershell\getsiteowners.ps1:8 char:79
    + foreach-object -process {$y=stsadm -o enumsites -url $_.Default;$sites=[xml]$y; <<<< $sites.Sites.Site}|

  5. rolandserman January 29, 2009 at 6:37 pm

    Do me a favor, from your powershell prompt type the following:

    stsadm -help enumsites

    Do you get the help for enumsites? or do you get an error about not recognizing stsadm as a cmdlet?

  6. Mike January 29, 2009 at 6:44 pm

    I get the help options:
    PS E:\Software\Powershell> stsadm -help enumsites

    stsadm.exe -o enumsites
    -url
    -showlocks
    -redirectedsites
    [-databasename ]

  7. rolandserman January 29, 2009 at 7:01 pm

    Actually, after re-reading your error (The following elements are not closed), it looks like you’re missing a bracket. Give me a few minutes to pull this over to one of my SharePoint servers, and make sure I didn’t miss something when I pasted it into my blog.

  8. rolandserman January 29, 2009 at 7:05 pm

    hmmm, are you running this on Server 2003, or Server 2008?

  9. rolandserman January 29, 2009 at 8:49 pm

    By the way, if you’re running the script on 2008, you need to run Powershell as Administrator. Otherwise it will fail with a bunch of misleading errors.

  10. Mike January 29, 2009 at 9:12 pm

    This is Server 2003

  11. JohnBrown September 25, 2009 at 8:29 pm

    I just tried your script and got this:

    The term ‘c:\AllSiteUsers.csv’ is not recognized as a cmdlet, function, operable program, or script file. Verify the te
    rm and try again.
    At E:\Software\powershell\getsiteowners.ps1:14 char:32
    + $filename = c:\AllSiteUsers.csv <<<<
    Out-File : Cannot bind argument to parameter 'FilePath' because it is null.
    At E:\Software\powershell\getsiteowners.ps1:15 char:75
    + Write-Output "Site Collection,Title,Group,UserID,Email Address" | Out-File <<<< $filename -Append

    I set executionpolicy to unrestricted, and I get help for stsadm -help enumsites.

    Any other suggestions you might have would be hugely appreciated!

  12. Alowishus January 26, 2010 at 6:16 pm

    @Mike

    You are most likely receiving the error because there is an issue with a line on your enumsites results.

    View enumsites results to verify there is not a line error somewhere.

  13. MacgeesMaster March 11, 2010 at 5:35 pm

    Hey man – thanks for your site, it helped alot. I’ve adjusted your program slightly so that it would include site administrators and groups that were empty.

    Here is the new code:

    #*###########################################################################
    #Assumptions:
    #-Running on machine with WSS/MOSS
    #-C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN in path
    #-Using Powershell v2
    #-To run this program
    # 1) Open up Powershell.
    # 2) Navigate to the directory where the script is (in powershell)
    # 3) Type .\Nameofprogram.ps1 and press enter
    #
    # Create by:
    # Program was found on site: http://rolandserman.wordpress.com/2008/08/27/list-site-owners-for-all-site-collections/
    #
    # Purpose:
    # To find out who are all the site users within a Sharepoint farm.
    #
    # Things to do:
    #
    #
    #
    #
    ##########################################################################*#

    ##########################################################################*#
    #
    # Include
    # Any other ps1 programs to be inluded
    #
    ##########################################################################*#

    ####################################################################################
    #
    # Main script
    #
    ####################################################################################
    # Preamble
    cls;
    write-host “###################################################################################”

    # List the GUIDs of all web apps on this farm
    #Get-SPWebApplication | ft name, id;

    #write-host “getting farm information”
    #Get-SPFarm;

    #Generates a list of all site collections across all Web Applications,
    #and outputing them into an array
    write-host “1.0 Start”

    $sitepath = New-Object System.Collections.ArrayList
    $output=stsadm -o enumzoneurls
    $x=[xml]$output
    $x.ZoneUrls.Collection|
    foreach-object -process {
    $y=stsadm -o enumsites -url $_.Default;$sites=[xml]$y;$sites.Sites.Site
    }|
    foreach-object -Process {
    $sitepath=$sitepath + $_.Url
    }

    $sitepath | Write-Output

    #Define Output file, and labels
    write-host “2.0 Define Output file, and labels”

    $filename = “AllSiteUsers.csv”;
    Write-Output “Site Collection,Title,Group,UserID,Email Address” | Out-File $filename -Append

    #Loops through all site collections generating a list of Site Owners
    write-host “3.0 Loops”

    [System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)

    foreach ($object in $sitepath) {
    #Create a new object of type Microsoft.SharePoint.SPSite
    $site=new-object Microsoft.SharePoint.SPSite($object)

    $web=$site.Rootweb
    #Determine site administrators
    foreach($user in $web.SiteAdministrators)
    {
    Write-Output “$($object),$web,***SiteAdministrators,$user,$($user.Email)” | Out-File $filename -Append
    $user
    $usercount = $usercount+1;
    }

    #Write out all users at root level
    foreach($user in $web.users)
    {
    Write-Output “$($object),$web,***RootGroup,$user,$($user.Email)” | Out-File $filename -Append
    $user
    $usercount = $usercount+1;
    }

    #Next deermine what users are contained in a group
    #$groups = $web.groups | ? {$_.Name -match “^.*Owners” }
    $groups = $web.groups;

    foreach($group in $groups)
    {
    $usercount = 0;

    foreach($user in $group.users)
    {
    Write-Output “$($object),$web,$($group.Name),$user,$($user.Email)” | Out-File $filename -Append
    $user
    $usercount = $usercount+1;
    }

    #If a group is empty still record it but as empty
    if ($usercount -eq 0){
    Write-Output “$($object),$web,$($group.Name),***EmptyGroup” | Out-File $filename -Append
    }
    }

    }

    #Converts the csv ouput to XML
    write-host “4.0 Converts the csv ouput to XML”
    Import-Csv AllSiteUsers.csv | Export-Clixml AllSiteUsers.xml

  14. MacgeesMaster March 11, 2010 at 5:38 pm

    Missed one thing, I meant to say “I’ve adjusted your program slightly so that it would include site administrators, groups that were empty,” AND users at the Root level of a site.

  15. sal April 27, 2010 at 4:00 am

    Hi, I tried running your script but I am receiving this error:

    Out-File : Cannot bind argument to parameter ‘FilePath’ because it is null.

    At C:\Users\~service-account\AllSiteOwners.ps1:28 char:78

    + Write-Output “$($object),$web,$($group.Name),$user,$($user.Email)” | Out-File

    <<<< $filename -Append

    + CategoryInfo : InvalidData: (:) [Out-File], ParameterBindingVal

    idationException

    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.OutFileCommand

  16. SPAdmin June 3, 2011 at 5:21 pm

    I adjusted a few items. Filename needed to be inclosed in quotes which is solves the error that Sal was having. I also added a critical $site.Dispose() at the end of the loop that opens a new site object. Without this you start generate a memory leak in the application pool. Not a big deal for small sites but without it on a large topology could be fatal.

    #Generates a list of all site collections across all Web Applications,
    #and outputing them into an array

    $sitepath = New-Object System.Collections.ArrayList
    $output=stsadm -o enumzoneurls
    $x=[xml]$output
    $x.ZoneUrls.Collection|
    foreach-object -process {$y=stsadm -o enumsites -url $_.Default;$sites=[xml]$y;$sites.Sites.Site}|
    foreach-object -Process {$sitepath=$sitepath + $_.Url}
    $sitepath | Write-Output

    #Define Output file, and labels

    $filename = “AllSiteUsers.csv”
    Write-Output “Site Collection,Title,Group,UserID,Email Address” | Out-File $filename -Append

    #Loops through all site collections generating a list of Site Owners

    [System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)
    foreach ($object in $sitepath) {
    $site=new-object Microsoft.SharePoint.SPSite($object)
    $web=$site.Rootweb
    $groups = $web.groups | ? {$_.Name -match “^.*Owners” }
    foreach($group in $groups) {
    foreach($user in $group.users) {
    Write-Output “$($object),$web,$($group.Name),$user,$($user.Email)” | Out-File $filename -Append
    $user
    $site.Dispose()
    }
    }
    }

    #Converts the csv ouput to XML

    Import-Csv AllSiteUsers.csv | Export-Clixml AllSiteUsers.xml

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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: