Solved

SharePoint Enumerate Groups and Users Script

Posted on 2014-02-25
10
1,201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 32

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 32

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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 32

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 32

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 32

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
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 …
Suggested Courses

624 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