RSS

Tag Archives: SharePoint

Using PowerShell list SharePoint site users, roles, groups on all sites

I have been asked to generate the list of users with their roles and groups belongs to for all sites presented in the web application. I found some blog talking about same requirements. But it is not fully satisfied my requirements. So I have taken his logic and created the PowerShell script to list the users, their roles and groups belong for the sites presented in the web application

Here is the below code which will iterate all sites and enumerate the users with their roles and groups belongs to.

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”) > $null
function EnumerateUserRolesPermissions ([string]$webURL){
$site = new-object Microsoft.SharePoint.SPSite($webURL)
$web = $site.OpenWeb()
$webUsers = $web.Users
$groups = $web.sitegroups
foreach($webUser in $webUsers){
$Permissions = $web.Permissions
foreach($group in $groups)
{
foreach($Permission in $Permissions){
if($webUser.ID -eq $Permission.Member.ID){
foreach ($role in $webUser.Roles){
if ($role.Type -ne [Microsoft.SharePoint.SPRoleType]::None)
{
write-host $webURL,“;“,$webUser.LoginName,“;“,$webUser.Name,“;",$role.Type.ToString(),";",$webUser.groups
}
}
}
if($group.ID -eq $Permission.Member.ID){
foreach ($role in $group.Roles){
if ($role.Type -ne [Microsoft.SharePoint.SPRoleType]::None
{
foreach($user in $group.users){
write-host $webURL,“;“,$user.LoginName,“;“,$user.Name,“;",$role.Type.ToString(),";",$group.name
}
}
}
}
}
}
}
}
function EnumerateSiteUsers ()
{
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
foreach ($spService in $farm.Services) {
if (!($spService -is [Microsoft.SharePoint.Administration.SPWebService])) {
continue;
}
foreach ($webApp in $spService.WebApplications) {
if ($webApp -is [Microsoft.SharePoint.Administration.SPAdministrationWebApplication]) { continue }
$webAppUrl = $webApp.GetResponseUri('Default').AbsoluteUri
foreach ($site in $webApp.Sites) {
foreach ($web in $site.AllWebs) {
EnumerateUserRolesPermissions $web.url
}
}
}
}
}

Hope this helps

Advertisements
 
14 Comments

Posted by on February 3, 2011 in PowerShell, Sharepoint

 

Tags: ,