Thursday, December 15, 2016

O365 Powershell script to get SharePoint Web Groups and its Count

Add-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
 Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell
}


$username = "username@microsoft.com"
$password = "12345"
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force

Add-Type -Path "D:\Dlls\Microsoft.SharePoint.Client.dll"
Add-Type -Path "D:\Dlls\Microsoft.SharePoint.Client.Runtime.dll"
#Get the Client Context and Bind the Site Collection


function Get-SPGroups()
{

//Load your sites as input file
$getSites= Get-content "D:\Scripts\Sites.txt"

foreach($webUrl in $getSites)
{

try{

$context = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)
#Authenticate
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$context.Credentials = $credentials
$web = $context.Web;
$context.Load($web)
$context.ExecuteQuery()
Write "Url: $webUrl : $_" >>D:\Scripts\UrlProcessed.txt
}
catch{
Write "Error: $webUrl : $_" >>D:\Scripts\Error.txt -Append
continue;
}


$ListItemCollection = @()
$groups = $web.RoleAssignments.Groups
$context.Load($groups);
$context.ExecuteQuery()

foreach($grp in $groups) {


$data = @{
                        "Site URL" = $webUrl
                        "Site Title" = $web.Title
                        "Group Name" = $grp.Title
"Group Count" = $groups.Count                                      
}

New-Object PSObject -Property $data | Select "Site URL", "Site Title","Group Name", "Group Count"



}

}

$context="";



}

Get-SPGroups  | Export-Csv -NoTypeInformation -Path D:\Scripts\Sites1.csv

No comments:

Post a Comment