Date and Month display

Get Site Collection Administrator and Site Owners List across Tenant/Office 365 Level

<#
******-----------------------------------------------------------------------******
Author          -> Shiv Mangal Singh
Date            -> 10th April - 2017
Description     -> This Script will generate to get SCA and Site Owner User's List to all SharePoint online site collection across Tenant Level
Path of csv file --> $FileUrl ="D:\shiv\Powershell\SiteOwner_Tenant_9thSMSthApril.csv"
                 --> $currentLogPath ="D:\shiv\Powershell\testlog.csv"
Tenant Site URL  --> $adminURL ="https://Office365.contoso.com"

******-----------------------------------------------------------------------******
 #>
#Import-Module ‘C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell’ -DisableNameChecking

Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "D:\shiv\Powershell\DLL\Microsoft.SharePoint.Client.UserProfiles.dll"

#Required Parameters
$adminURL = "https://Office365.contoso.com"
$sUsername = "ABC@contoso.onmicrosoft.com"
$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString  

#Connect to SharePoint Online
  #SPO Client Object Model Context
        $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($adminURL)
        $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUsername, $sPassword)  
        $spoCtx.Credentials = $spoCredentials

        $spoTenant= New-Object Microsoft.Online.SharePoint.Tenantadministration.Tenant($spoCtx)
        $spoTenantSiteCollections=$spoTenant.GetSiteProperties(0,$true)
        $spoCtx.Load($spoTenantSiteCollections)
        $spoCtx.ExecuteQuery()
# File Directory for reports and logs
$FileUrl ="D:\shiv\Powershell\owner_9June17.csv"
$currentLogPath ="D:\shiv\Powershell\log__9June17.csv"
# Headers for the data
“Site URL `t Site Created `t Primary SCA`t SCA Users `t SCA Count `t Owner Group`t Owner Group Members `t Owners Count ” | Out-File $FileUrl

#Logs and prints messages
function LogMessage([String] $Msg)
{
    Write-Host $Msg -ForegroundColor Cyan
    Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") Message: $Msg" | Out-File -FilePath $currentLogPath -Append -Force
}
#Logs and prints error messages
function LogError([String] $ErrorMessage, [String]$ErrorDetails, [String]$ErrorPosition)
{
    Write-Host $ErrorMessage -foregroundcolor red
    $fullErrorMessage = $ErrorMessage + $ErrorDetails + ". " + $ErrorPosition
    Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") ERROR: $fullErrorMessage" | Out-File -FilePath $currentLogPath -Append -Force
}

#Get all Sites (Root sites only)
foreach($site in $spoTenantSiteCollections)
{
try
 {
 # Skip Mydrive site from Report
if($site.Url -ne "https://xyz.connect.contoso/")
{
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($site.Url)
$ctx.Credentials =   $spoCredentials
$rootsite= $ctx.Web
$ctx.Load($rootsite)
$ctx.ExecuteQuery()

$siteCreated = $rootsite.Created
$siteURL = $site.Url
$siteOwner = $site.Owner

#Get all users with Site Collection administrator permissions
$siteadmins = Get-SPOUser -Site $siteURL -Limit All | select LoginName, IsSiteadmin | ? { $_.ISSiteadmin }

#Collect all site collection administrators (user accounts) to a variable $siteUsers and site collection administrator count to $scaCount
$siteUsers = @()
$scaCount =@()
foreach($siteadmin in $siteadmins)
{
$siteUsers += $siteadmin.LoginName + ” ; ”
$scaCount =$siteUsers.Count
}

#Get all Groups from the site collection permissions
$sitegroups = Get-SPOSiteGroup -Site $siteURL

#Get Group info and members that have site owners permissions
foreach ($sitegroup in $sitegroups)
{
$i = 0
foreach($role in $sitegroup.Roles)
{
#Get all users with Site Owner permissions and Full control groups permission
if ($role.Contains(“Site Owner”) -or $role.Contains(“Full Control”) )
{
$i = $i + 1

$GroupOwneruser = @()
#Collect all Site owner accounts to a variable $GroupOwneruser
foreach($user in $sitegroup.Users)
{
    $GroupOwneruser += $user + ";"
  write-host  $GroupOwneruser `t $GroupOwneruser.Count  -ForegroundColor Green
}

    if ($i -gt 1)
    {
    "`t `t `t`t `t" + $sitegroup.Title + "`t" + $GroupOwneruser + "`t" + $GroupOwneruser.Count | Out-File Out-File $FileUrl -Append
    }
    else
    {

    "$siteURL `t $siteCreated `t $siteOwner`t $siteUsers `t $scaCount `t" + $sitegroup.Title + "`t" + $GroupOwneruser + "`t" + $GroupOwneruser.Count  | Out-File $FileUrl -Append
    }
 
    }
    }
    }
    }
    }
   catch
        {
        # Exception handling in log file
            if($_.Exception.Message -like '*(401) Unauthorized*' -or $_.Exception.Message -like '*Access denied*')
            {
                LogMessage("You need permission to access this site: "+ $site.Url)
            }
            else
            {
                LogError $_.Exception.Message $_.Exception.GetType().FullName $_.InvocationInfo.PositionMessage
            }
        }


}
<#***    The END (Users Permission list across Tenant level)  #>

No comments:

Post a Comment