Solved

SharePoint Enumerate Groups and Users Script

Posted on 2014-02-25
10
1,174 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This collection of functions covers all the normal rounding methods of just about any numeric value.
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 …
Viewers will learn the different options available in the Backstage view in Excel 2013.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

685 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