Solved

SharePoint Enumerate Groups and Users Script

Posted on 2014-02-25
10
1,135 Views
Last Modified: 2014-02-27
I am trying to create a script to enumerate the groups within sharepoint 2010 and the members of each group and output it to a file.

i have written this so far:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = New-Object Microsoft.SharePoint.SPSite("https://site name")
$groups = $site.RootWeb.sitegroups
foreach ($grp in $groups) {"Group: " + $grp.name; foreach ($user in $grp.users) {"  User: " + $user.name} }

which throws an error at: foreach ($user in $grp.users) {"  User: " + $user.name} }

the error is:
The following exception was thrown when trying to enumerate the collection: "<n
ativehr>0x80070005</nativehr><nativestack></nativestack>".
At line:1 char:58
+ foreach ($grp in $groups) {"Group: " + $grp.name; foreach <<<<  ($user in $gr
p.users) {"  User: " + $user.name} }
    + CategoryInfo          : NotSpecified: (:) [], ExtendedTypeSystemException + FullyQualifiedErrorId : ExceptionInGetEnumerator

if i remove the part where it errors and just run
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = New-Object Microsoft.SharePoint.SPSite("https://site name")
$groups = $site.RootWeb.sitegroups
foreach ($grp in $groups) {"Group: " + $grp.name} }

it works and enumerates all groups.

none of it outputs to a file as yet.

any help would be greatly appreciated.
0
Comment
Question by:Mark Wood
  • 5
  • 5
10 Comments
 
LVL 31

Expert Comment

by:Jamie McAllister MVP
ID: 39888114
0
 
LVL 2

Author Comment

by:Mark Wood
ID: 39888631
i saw that one but couldn't get it to output anything.

not sure what i am missing.

any help would be awesome
0
 
LVL 31

Expert Comment

by:Jamie McAllister MVP
ID: 39888673
OK, your script works fine for me.

First I thought that $user.name was missing a semicolon, but actually the script worked with or without that.

I ran the Powershell console as Admin. Did you?
0
 
LVL 31

Expert Comment

by:Jamie McAllister MVP
ID: 39888717
Not that it should matter, but I also broke it onto different lines;

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = New-Object Microsoft.SharePoint.SPSite("http://MyUrl")
$groups = $site.RootWeb.sitegroups
foreach ($grp in $groups) 
{"Group: " + $grp.name; 
	foreach ($user in $grp.users) 
	{"  User: " + $user.name} 
} 

Open in new window

0
 
LVL 2

Author Comment

by:Mark Wood
ID: 39888753
i edited it to be
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = New-Object Microsoft.SharePoint.SPSite("https://site name")
$groups = $site.RootWeb.sitegroups
foreach ($grp in $groups) {"Group: " + $grp.name; foreach ($user in $web.users) {"  User: " + $user.name} }

and i get a list of users and groups. i havent been able to get it to output to a file as yet.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 31

Expert Comment

by:Jamie McAllister MVP
ID: 39888792
0
 
LVL 2

Author Comment

by:Mark Wood
ID: 39889018
ok i am at a loss and now my core script isn't working
0
 
LVL 2

Author Comment

by:Mark Wood
ID: 39889888
ok .. i have re-worked the script as follows:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null

#Instantiate the SPSite class for the root-level site collection
$rootsite = New-Object Microsoft.SharePoint.SPSite("https://mysite")

#Assign the SPGroupCollection containing all the groups in the site collection to a variable
$rootgroups = $rootsite.RootWeb.SiteGroups


#Uncomment the following line to get a tabular presentation of group names and user counts
$rootgroups | Select-Object -Property Name, @{Name="Number of Users";Expression = {$_.Users.Count}}


#Instantiate necessary objects and start writing to XML output
$allprincipals = ""
$textwriter = New-Object -typeName "System.IO.StringWriter"
$xmlwriter = New-Object -typeName "System.Xml.XmlTextWriter" -argumentList $textwriter
$xmlwriter.Formatting = [System.Xml.Formatting]::Indented
$xmlwriter.WriteStartElement("SiteGroups")
$xmlwriter.WriteAttributeString("Count",$rootgroups.Count)

#Output each group's information
foreach ($group in $rootgroups)

    {
        $xmlwriter.WriteStartElement("Group")
        $xmlwriter.WriteAttributeString("Name",$group.Name)
        $xmlwriter.WriteAttributeString("MemberCount",$group.Users.Count)
       
        #Output each user's information
        foreach ($user in $group.Users)
{
                $xmlwriter.WriteStartElement("Member")
                $xmlwriter.WriteAttributeString("Name",$user.Name)
                $xmlwriter.WriteAttributeString("Login",$user.LoginName)
                #SPUser object is not disposable, so nullify the variable instead
                $user = $null
                $xmlwriter.WriteEndElement()
            }
        #SPGroup object is not disposable, so nullify the variable instead
        $group = $null
        $xmlwriter.WriteEndElement()

}

#Call Close on the XML writer - this automatically closes all open tags
$xmlwriter.Close()

#Convert the Text Writer object to string and send to standard output
$textwriter.ToString()

it gives me what i am looking for but it ouly ouputs to the screen not a file.

if you could help me get the output to a file it would be greatly appreciated.
0
 
LVL 31

Accepted Solution

by:
Jamie McAllister MVP earned 500 total points
ID: 39891636
To write to file you just need to change the last line like so;

$textwriter.ToString() > 'C:\temp\output.txt'
0
 
LVL 2

Author Closing Comment

by:Mark Wood
ID: 39892585
Thanks so much for all your help. i was drawing a complete blank.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
Viewers will learn the different options available in the Backstage view in Excel 2013.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now