SharePoint Pals
 | Sign In
Powershell Tips Series Part 1 : Script to get Sile collection details and sites with unique permission in SharePoint Online
by Sriram Varadarajan 3 Oct 2015
Author
Enterprise Architect
Visits   
Today  :   11     Total  :    5736

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 Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" 
 		#Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.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
 
blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation