Solved

SharePoint Enumerate Groups and Users Script

Posted on 2014-02-25
10
1,162 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
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: 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
 
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

The canonical version of this article is on my web site here: http://iconoun.com/articles/collisions/ A companion presentation is available here: http://iconoun.com/articles/collisions/Unicode_Presentation.pdf
User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

790 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