Thursday, December 14, 2017

Get List of Site owners from all web applications in SharePoint using Powershell

List of Site owners from all web applications in SharePoint using Powershell

Add-PSSnapin "Microsoft.SharePoint.Powershell"


function CreateReportObject($url = $null, $title = $null, $desc = $null, $IsRoot = $null, $groupname = $null, $loginname = $null, $dg = $null, $email = $null, $ucount = $null, $created = $null, $farm = $null,$isreadonly = $null) 

$newreportobj = new-object psobject 
        $newreportobj | add-member noteproperty -name "URL" -value $url 
$newreportobj | add-member noteproperty -name "Title" -value $title 
$newreportobj | add-member noteproperty -name "Description" -value $desc 
$newreportobj | add-member noteproperty -name "IsRootWeb" -value $IsRoot 
        $newreportobj | add-member noteproperty -name "Groups" -value $groupname
        $newreportobj | add-member noteproperty -name "Owners" -value $loginname
$newreportobj | add-member noteproperty -name "DomainGroups" -value $dg
$newreportobj | add-member noteproperty -name "Farm" -value $farm
$newreportobj | add-member noteproperty -name "ITO" -value ''
$newreportobj | add-member noteproperty -name "Owners Email" -value $email
$newreportobj | add-member noteproperty -name "Additional Owners" -value ''
$newreportobj | add-member noteproperty -name "User Count" -value $ucount
$newreportobj | add-member noteproperty -name "Created" -value $created 
$newreportobj | add-member noteproperty -name "Is Read-Only" -value $isreadonly
        return ($newreportobj) 
}

#Get all members of the domain group
function GetDomainGroupUsers($web = $null, $domaingroup = $null) 

$_domaingroupusers = "" 
        $reachedMax = $false
$adusers = [Microsoft.SharePoint.Utilities.SPUtility]::GetPrincipalsInGroup($web, $domaingroup, 50, [ref]$reachedMax)
foreach ($aduser in $adusers) 
{
if($aduser.Email -ne '' -AND $aduser.LoginName -like '*i:0#.w*') { $_domaingroupusers += "$($aduser.LoginName);" }
}
        return ($_domaingroupusers) 
}

$global:_Loginname = ""
$global:_dg = ""
$global:_email = ""

#Add all users to the "user" variable
function AddUsers($user) 

if($user.Email -ne '' -AND $user.LoginName -like '*i:0#.w*') { 
if($global:_Loginname -notlike "*$($user.LoginName);*")
{
$global:_Loginname += "$($user.LoginName);" 
$global:_email += "$($user.Email);"
}
}
else { 
if($user.IsDomainGroup -eq 'True') { 
if($global:_dg -notlike "*$($user.Name);*")
{
$global:_dg += "$($user.Name);" 
}

}
}

$fName = [System.IO.Path]::GetRandomFileName() + ".csv"

$ReportPath =".\" + $fName; 

$SiteReportCollection= @() 


$SiteReportCollection+=CreateReportObject -url $date -title '' -desc '' -IsRoot '' -groupname '' -loginname '' -dg '' -email '' -ucount '' -created '' -farm '' -isreadonly ''

$WebAppUrls = "http://webapplication1.com;http://webapplication2.com"

$WebApps = $WebAppUrls.split(";")

foreach($WebApp in $WebApps){

if($WebApp){

$_farm = ""

$webs = Get-SPWebApplication $WebApp | Get-SPSite -Limit All | Get-SPWeb -Limit All | where { $_.IsAppWeb -ne 'True' }
  
#Iterate through all the sub sites except the app web
foreach($web in $webs){

$webGroups = $web.Groups;   
$_IsRootWeb = $web.IsRootWeb
$_Groups = ""
$global:_Loginname = ""
$global:_dg = ""
$global:_email = ""
$_usercount = $web.Users.Count

foreach($webGroup in $webGroups)
{
$_usercount += $webGroup.Users.Count
}

#Get all the users and groups having FULLMASK BasePermission 
foreach($rAssignment in $web.RoleAssignments)
{

foreach($roleDefinition in $rAssignment.RoleDefinitionBindings | Where {$_.BasePermissions -eq [Microsoft.SharePoint.SPBasePermissions]::FullMask})
{
$member = $rAssignment.Member

if($member.GetType() -eq [Microsoft.SharePoint.SPUser])
{
AddUsers $member
}
else
{
if($member.GetType() -eq [Microsoft.SharePoint.SPGroup])
{
foreach ($user in $member.Users) 
{
AddUsers $user
}

$_Groups += "$($member.Name);"
}
}

}

}

#Get all the users from groups having full control or owner permission
foreach($mywebGroup in $webGroups |  foreach { $Groups = $_; $_ } | where {$_.Roles -like '*owner*'} )
{
foreach ($user in $mywebGroup.Users) 
{
AddUsers $user
}

$_Groups += "$($mywebGroup.Name);"
}

#Get all the full control users directly added to the site
foreach($user in $web.Users | foreach { $users = $_; $_ } | where {$_.Roles -like '*owner*'})

AddUsers $user


$SiteReportCollection += CreateReportObject -url $web.Url -title $web.Title -desc $web.Description -IsRoot $_IsRootWeb -groupname $_Groups -loginname $global:_Loginname -dg $global:_dg -email $global:_email -ucount $_usercount -created $web.Created -farm $_farm -isreadonly $web.Site.ReadOnly

$web.Dispose();
}

}
  
}
  
$SiteReportCollection+=CreateReportObject -url $date -title '' -desc '' -IsRoot '' -groupname '' -loginname '' -dg '' -email '' -ucount '' -created '' -farm '' -isreadonly ''

$SiteReportCollection | export-csv $ReportPath -notypeinformation

Write-host "SharePoint site owners data is exported to a CSV file at:"$ReportPath -ForegroundColor Green 

Write-Host "Press any key to continue ..."


$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

No comments:

Post a Comment