Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $newWeb = New-Object Microsoft.SharePoint.Client.WebCreationInformation
- $newWeb.Url = 'someURL'
- $newWeb.UseSamePermissionsAsParentSite = $false
- $newWeb.Title = 'Some Web'
- $newWeb.WebTemplate = "STS#0"
- $spWeb = $ctx.Web.Webs.Add($newWeb)
- $ctx.Load($spWeb)
- $ctx.ExecuteQuery()
- function Set-SPOListBreakRoleInheritance
- {
- param (
- [Parameter(Mandatory=$true,Position=1)]
- [string]$Username,
- [Parameter(Mandatory=$true,Position=2)]
- [string]$Url,
- [Parameter(Mandatory=$true,Position=3)]
- [string]$AdminPassword,
- [Parameter(Mandatory=$false,Position=4)]
- [bool]$IncludeSubsites=$false
- )
- $password = ConvertTo-SecureString -string $AdminPassword -AsPlainText -Force
- $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
- $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $password)
- $ctx.Load($ctx.Web.Lists)
- $ctx.Load($ctx.Web)
- $ctx.Load($ctx.Web.Webs)
- $ctx.ExecuteQuery()
- Write-Host
- Write-Host $ctx.Url -BackgroundColor White -ForegroundColor DarkGreen
- foreach( $ll in $ctx.Web.Lists)
- {
- $ll.BreakRoleInheritance($true, $false)
- $ll.Update()
- try
- {
- $ctx.ExecuteQuery()
- Write-Host "Removed inherited permissions for " $ll.Title
- }
- catch
- {
- Write-Host "Failed to remove permissions for " $ll.Title
- }
- }
- if($ctx.Web.Webs.Count -gt 0 -and $IncludeSubsites)
- {
- for($i=0; $i -lt $ctx.Web.Webs.Count ; $i++)
- {
- Set-SPOListBreakRoleInheritance -Url ($ctx.Web.Webs[$i].Url) -Username $Username -AdminPassword $AdminPassword -IncludeSubsites $IncludeSubsites
- }
- }
- }
- # Paths to SDK. Please verify location on your computer.
- Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll"
- Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
- Set-SPOListBreakRoleInheritance -Username "trial@trialtrial123.onmicrosoft.com" -Url "https://trialtrial123.sharepoint.com" -AdminPassword "Pass" -IncludeSubsites $false
- [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
- [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
- Function Get-ClientContext([string]$Url,[string]$UserName,[string]$Password)
- {
- $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
- $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
- $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
- return $context
- }
- Function Web-BreakInheritance([Microsoft.SharePoint.Client.Web]$Web)
- {
- $Web.BreakRoleInheritance($true, $false)
- $Web.Context.ExecuteQuery()
- }
- $UserName = "jdoe@contoso.onmicrosoft.com"
- $Password = Read-Host -Prompt "Enter the password"
- $Url = "https://contoso.sharepoint.com/blog"
- $context = Get-ClientContext -Url $Url -UserName $UserName -Password $Password
- Web-BreakInheritance -Web $context.Web
- $context.Dispose()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement