Wednesday, December 13, 2017

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

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

Add-PSSnapin "Microsoft.SharePoint.Powershell"

function CreateReportObject($url = $null, $title = $null, $loginname = $null, $dg = $null, $created = $null, $isreadonly = $null, $email = $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 "Owners" -value $loginname
$newreportobj | add-member noteproperty -name "DomainGroups" -value $dg
        $newreportobj | add-member noteproperty -name "Email" -value $email
$newreportobj | add-member noteproperty -name "Created" -value $created 
$newreportobj | add-member noteproperty -name "Is Read-Only" -value $isreadonly
        return ($newreportobj) 
}

$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 '' -loginname '' -dg '' -created '' -isreadonly '' -email ''

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

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

foreach($WebApp in $WebApps){

if($WebApp){

$sites = Get-SPWebApplication $WebApp | Get-SPSite -Limit All

#Iterate through all the sub sites 
foreach($site in $sites)
{
$global:_Loginname = ""
$global:_dg = ""
            $global:_email = ""

foreach($siteAdmin in $site.RootWeb.SiteAdministrators)
{
AddUsers $siteAdmin
}

$SiteReportCollection += CreateReportObject -url $site.Url -title $site.RootWeb.Title -loginname $global:_Loginname -dg $global:_dg -created $site.RootWeb.Created -isreadonly $site.ReadOnly -email $global:_email

$site.Dispose()
}

}

}

$SiteReportCollection+=CreateReportObject -url $date -title '' -loginname '' -dg '' -created '' -isreadonly '' - email''

$SiteReportCollection | export-csv $ReportPath -notypeinformation

Write-host "SharePoint site collection owner list 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