Solved

SharePoint Enumerate Groups and Users Script

Posted on 2014-02-25
10
1,145 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Back in July, I blogged about how Microsoft's new server pricing model, combined with the end of the Small Business Server package, would result in significant cost increases for many small businesses (see SBS End of Life: Microsoft Punishes Small B…
Many companies are making the switch from Microsoft to Google Apps (https://www.google.com/work/apps/business/). Use this article to learn more about what Google Apps has to offer and to help if you’re planning on migrating to Google Apps. It is …
Viewers will learn the different options available in the Backstage view in Excel 2013.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

930 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

9 Experts available now in Live!

Get 1:1 Help Now