Powershell Tips Series Part 1 : Script to get Sile collection details and sites with unique permission in SharePoint Online

Sriram Varadarajan
 
Solution Architect
October 3, 2015
 
Rate this article
 
Views
9088

As of now, I am more and more involved into automation of regular SharePoint activities into PowerShell scripts. Not all things are that easy to find out in MSDN and technet. I would be sharing scripts to the community through this series that would make the life easier to keep track of the scripts that would be essential for SharePoint development and administration activities

 

Script to fetch all the site collection details in SharePoint online

 Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking -ErrorAction SilentlyContinue
 
 Connect-SPOService -Url "https://XXXX-admin.sharepoint.com" -Credential userid@example.com
 
 Get-SPOSite –Detailed | Export-CSV –LiteralPath “Path/filename.csv” –NoTypeInformation
 

 

Script to get all the site and sub site which are having unique permission in sharepoint online

 ############################################################################################################################################ 
 #Script that gets all the property Bags  
 # Required Parameters: 
 #  -> $sUserName: User Name to connect to the SharePoint Online Site Collection. 
 #  -> $sPassword: Password for the user. 
 #  -> $sSiteUrl: SharePoint Online Site Url 
 ############################################################################################################################################ 
  
 $host.Runspace.ThreadOptions = "ReuseThread" 
  
 function Invoke-LoadMethod() {
 param(
    [Microsoft.SharePoint.Client.ClientObject]$Object = $(throw "Please provide a Client Object"),
    [string]$PropertyName
 ) 
    $ctx = $Object.Context
    $load = [Microsoft.SharePoint.Client.ClientContext].GetMethod("Load") 
    $type = $Object.GetType()
    $clientLoad = $load.MakeGenericMethod($type) 
 
 
    $Parameter = [System.Linq.Expressions.Expression]::Parameter(($type), $type.Name)
    $Expression = [System.Linq.Expressions.Expression]::Lambda(
             [System.Linq.Expressions.Expression]::Convert(
                 [System.Linq.Expressions.Expression]::PropertyOrField($Parameter,$PropertyName),
                 [System.Object]
             ),
             $($Parameter)
    )
    $ExpressionArray = [System.Array]::CreateInstance($Expression.GetType(), 1)
    $ExpressionArray.SetValue($Expression, 0)
    $clientLoad.Invoke($ctx,@($Object,$ExpressionArray))
 }
  
 function getSubWebs
      {          
 	 param ($spoCtx, $path)
          try
          {
              $oWebsite = $spoCtx.Web;
              #$spoCtx.Load($oWebsite, $website => $website.Webs, $website => $website.Title);
              $spoCtx.Load($oWebsite);
 			 Write-Host "----------------------------B4------------------------------------------------"  -foregroundcolor Green
 			 $spoCtx.ExecuteQuery();
 			 Write-Host "-----------------------------A4-----------------------------------------------"  -foregroundcolor Green
              foreach ($orWebsite in $oWebsite.Webs) {
                  $newpath = $mainpath + $orWebsite.ServerRelativeUrl
                  getSubWebs -spoCtx $spoCtx -path $newpath
                  Write-Host $newpath + " - " + $orWebsite.Title
              }
          }
         catch [System.Exception] 
 		{ 
         write-host -f red $_.Exception.ToString()    
 		}	           
      }
  
 #Definition of the function that allows to read property bags in SharePoint Online 
 function ReadSPO-PropertyBags 
 { 
     param ($sSiteUrl,$sUserName,$sPassword) 
     try 
     {     
         #Adding the Client OM Assemblies         
         #Add-Type –Path "C:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.dll" 
 		#Add-Type –Path "C:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.Runtime.dll" 
  
         #SPO Client Object Model Context 
         $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl) 
         $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName, $sPassword)   
         $spoCtx.Credentials = $spoCredentials       
  
         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green 
         Write-Host "Reading PropertyBags values for $sSiteUrl !!" -ForegroundColor Green 
         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green 
         
 		$web = $spoCtx.Web 
 		$spoCtx.Load($web.Webs)
 		$spoCtx.ExecuteQuery()
 		
 		
 		foreach($web1 in $web.Webs){
 		Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green
 		Write-Host $web1.Title " - " $web1.Url " - " $web1.HasUniqueRoleAssignments -ForegroundColor Green 
 		Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green
 			$prop = $web1.AllProperties
 			$spoCtx.Load($prop)
 			$spoCtx.ExecuteQuery()
 			$keys=$prop.FieldValues.Keys 
         #$spoPropertyBagKeys 
 			foreach($key in $keys){ 
 				Write-Host $key "-" $prop[$key] -ForegroundColor Green 
 			}
 			Invoke-LoadMethod -Object $web1 -PropertyName "HasUniqueRoleAssignments"
 			
 			$spoCtx.ExecuteQuery()
             Write-Host  "HasUniqueRoleAssignments-" $web1.HasUniqueRoleAssignments -ForegroundColor Green
         } 
 		
 		#getSubWebs -spoCtx $spoCtx -path $sSiteUrl
 		      
         $spoCtx.Dispose() 
     } 
     catch [System.Exception] 
     { 
         write-host -f red $_.Exception.ToString()    
     }     
 } 
  
 #required parameters 
 $ssiteurl = "https://XXXX.sharepoint.com/sites/Dev018"
 $susername = "XXXX@example.com" 
 $spassword=convertto-securestring "MySecretPassword" -asplaintext -force
 $mainpath =  "https://XXXX.sharepoint.com/sites/Dev018"
 
 ReadSPO-PropertyBags -sSiteUrl $sSiteUrl -sUserName $sUserName -sPassword $sPassword
 

Author Info

Sriram Varadarajan
 
Solution Architect
 
Rate this article
 
Sriram is a Technology Evangelist with 15+ years experience in Microsoft Technologies. He is an enterprise architect working for large pharmaceutical organization which has presence globally with largest Microsoft implementation ...read more
 

Leave a comment