SharePoint On Premise : Script to fetch the list of users having access to a SiteCollection, groups they are part of and subsites having access to
Below scirpt will help you to pull the above mentioned inventory from SharePoint site collection quickly to a CSV file.
#Load required dlls
if ((Get-PSSnapin
"Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq
$null) {
Add-PSSnapin"Microsoft.SharePoint.PowerShell"
}
#File references.
$csvClientsPath = $PSScriptRoot+"\ClientDetails.csv";
$dateTime = $(Get-Date -format u)-replace ':','_' ;
$logPath = $PSScriptRoot +'\ExportedUsers_'+$dateTime+'.csv' -replace '\s',''
#Result
$global:UserDataCollection = @();
function LogMessages()
{
param($clientId,$clientURL,$userLogin,$userDisplayName,$userEmail, $UserProjects,
$userGroupString)
$UserData = New-Object PSObject;
$UserData| Add-Member -type NoteProperty -name "ClientId" -value $clientId;
$UserData| Add-Member -type NoteProperty -name "ClientURL" -value $clientURL;
$UserData| Add-Member -type NoteProperty -name "UserLogin" -value $userLogin;
$UserData| Add-Member -type NoteProperty -name "DisplayName" -value $userDisplayName;
$UserData| Add-Member -type NoteProperty -name "EmailID" -value $userEmail;
$UserData| Add-Member -type NoteProperty -name "Projects" -value $UserProjects;
$UserData| Add-Member -type NoteProperty -name "Groups" -value $userGroupString;
$global:UserDataCollection += $UserData;
Write-Host $(Get-Date -format g) $UserData -ForegroundColor Yellow -backgroundcolor Blue;
}
function Import-UsersFromClient()
{
Import-Csv -Path $csvClientsPath |%{
#Loop CSV to get client ID and Client URL.
$web=$null;
$site=$null;
try{
if($_.ClientURL)
{
if([bool] ($site = Get-SPSite $_.ClientURL -ErrorAction SilentlyContinue) -eq $true) {
$web = $site.rootweb;
if($web)
{
$allUsers= $web.siteUsers;
#Loop thourgh all the users and pull the required information.
foreach($currUser in $allUsers)
{
$userGroups;
$userSites;
try{
$currSiteCollection= $null;
$rootWeb= $null;
try{
#Get the list of all the SharePoint Groups user is part of.
$userGroups = $currUser| select @{Name='Groups';Expression={$_.Groups -join '|'}}
#Get the list of subsites user has access to.
$token = $currUser.UserToken;
$currSiteCollection = new-object microsoft.sharepoint.spsite($_.ClientURL, $token);
$rootWeb = $currSiteCollection.RootWeb;
$accessWeb = $rootWeb.GetSubwebsForCurrentUser();
if($accessWeb.count-gt 0)
{
$userSites= $accessWeb.WebsInfo.ToArray().ServerRelativeUrl -join '|';
}
LogMessages -clientId $_.ClientId -clientURL $_.ClientURL -userLogin $currUser.UserLogin.ToString()
-userDisplayName $currUser.displayName.ToString() -userEmail
$currUser.Email.ToString() -UserProjects $userSites -userGroupString $userGroups.Groups.ToString()
}
catch{
LogMessages -clientId $clientId -clientURL $_.ClientURL -userLogin
$currUser.UserLogin.ToString()
-userDisplayName $currUser.displayName.ToString() -userEmail
$_.Exception.Message -UserProjects $_.Exception.Message -userGroupString
$_.Exception.Message
}
finally
{
if($rootWeb)
{
$rootWeb.Dispose();
}
if($currSiteCollection)
{
$currSiteCollection.Dispose();
}
}
}
catch{
LogMessages -clientId $clientId -clientURL $_.ClientURL -userLogin
$_.Exception.Message -userDisplayName $_.Exception.Message -userEmail
$_.Exception.Message -UserProjects $_.Exception.Message -userGroupString
$_.Exception.Message
}
}
}
}
}
}
catch{
LogMessages -clientId $_.Exception.Message -clientURL $_.Exception.Message -userLogin
$_.Exception.Message -userDisplayName $_.Exception.Message -userEmail
$_.Exception.Message -UserProjects $_.Exception.Message -userGroupString
$_.Exception.Message
}
finally
{
if($web){
$web.Dispose();
}
if($site){
$site.Dispose();
}
}
}
}
#Call Method
Import-UsersFromClient;
#Store Data To CSV.
$UserDataCollection | Export-Csv-LiteralPath $logPath -NoTypeInformation
Comments