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
January 29, 2009 at 4:59 pm
Tried running this and got an error, how do you run this via command line
January 29, 2009 at 6:09 pm
What was the error that you got? Also, this will only work if you run it via PowerShell.
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 <<<<
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.
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}|
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?
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 ]
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.
January 29, 2009 at 7:05 pm
hmmm, are you running this on Server 2003, or Server 2008?
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.
January 29, 2009 at 9:12 pm
This is Server 2003
September 3, 2009 at 10:27 pm
Cool site, love the info.