Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. $newWeb = New-Object Microsoft.SharePoint.Client.WebCreationInformation
  2. $newWeb.Url = 'someURL'
  3. $newWeb.UseSamePermissionsAsParentSite = $false
  4. $newWeb.Title = 'Some Web'
  5. $newWeb.WebTemplate = "STS#0"
  6. $spWeb = $ctx.Web.Webs.Add($newWeb)
  7. $ctx.Load($spWeb)
  8. $ctx.ExecuteQuery()
  9.  
  10. function Set-SPOListBreakRoleInheritance
  11. {
  12.  
  13. param (
  14. [Parameter(Mandatory=$true,Position=1)]
  15. [string]$Username,
  16. [Parameter(Mandatory=$true,Position=2)]
  17. [string]$Url,
  18. [Parameter(Mandatory=$true,Position=3)]
  19. [string]$AdminPassword,
  20. [Parameter(Mandatory=$false,Position=4)]
  21. [bool]$IncludeSubsites=$false
  22. )
  23.  
  24.  
  25.  
  26. $password = ConvertTo-SecureString -string $AdminPassword -AsPlainText -Force
  27. $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
  28. $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $password)
  29. $ctx.Load($ctx.Web.Lists)
  30. $ctx.Load($ctx.Web)
  31. $ctx.Load($ctx.Web.Webs)
  32. $ctx.ExecuteQuery()
  33. Write-Host
  34. Write-Host $ctx.Url -BackgroundColor White -ForegroundColor DarkGreen
  35. foreach( $ll in $ctx.Web.Lists)
  36. {
  37.  
  38. $ll.BreakRoleInheritance($true, $false)
  39. $ll.Update()
  40.  
  41. try
  42. {
  43. $ctx.ExecuteQuery()
  44. Write-Host "Removed inherited permissions for " $ll.Title
  45. }
  46. catch
  47. {
  48. Write-Host "Failed to remove permissions for " $ll.Title
  49. }
  50.  
  51.  
  52.  
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. if($ctx.Web.Webs.Count -gt 0 -and $IncludeSubsites)
  64. {
  65. for($i=0; $i -lt $ctx.Web.Webs.Count ; $i++)
  66. {
  67. Set-SPOListBreakRoleInheritance -Url ($ctx.Web.Webs[$i].Url) -Username $Username -AdminPassword $AdminPassword -IncludeSubsites $IncludeSubsites
  68. }
  69.  
  70. }
  71.  
  72.  
  73. }
  74.  
  75.  
  76. # Paths to SDK. Please verify location on your computer.
  77. Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll"
  78. Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
  79.  
  80.  
  81.  
  82. Set-SPOListBreakRoleInheritance -Username "trial@trialtrial123.onmicrosoft.com" -Url "https://trialtrial123.sharepoint.com" -AdminPassword "Pass" -IncludeSubsites $false
  83.  
  84. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
  85. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
  86.  
  87.  
  88. Function Get-ClientContext([string]$Url,[string]$UserName,[string]$Password)
  89. {
  90. $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
  91. $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
  92. $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
  93. return $context
  94. }
  95.  
  96.  
  97.  
  98. Function Web-BreakInheritance([Microsoft.SharePoint.Client.Web]$Web)
  99. {
  100. $Web.BreakRoleInheritance($true, $false)
  101. $Web.Context.ExecuteQuery()
  102. }
  103.  
  104. $UserName = "jdoe@contoso.onmicrosoft.com"
  105. $Password = Read-Host -Prompt "Enter the password"
  106. $Url = "https://contoso.sharepoint.com/blog"
  107.  
  108. $context = Get-ClientContext -Url $Url -UserName $UserName -Password $Password
  109. Web-BreakInheritance -Web $context.Web
  110. $context.Dispose()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement