Question

I have two scripts.

In the first script, I run through all of the sites and subsites in my tenant with PnP PowerShell, something like this:

$cred = Get-Credential
Connect-PnPOnline "https://tenant.sharepoint.com" -Credentials $cred
$SiteCollections = Get-PnPTenantSite

foreach($SiteCollection in $SiteCollections) {
  Connect-PnPOnline -Url $SiteCollection.Url -Credentials $cred

  $subwebs = Get-PnPSubWebs - Recurse
  if ($subwebs) {
    foreach($subweb in $subwebs) {
        Connect-PnPOnline -Url $subweb.Url -Credentials $cred
    }
  }
}

In the second script, I would like to disable two options in SharePoint Online which cannot be changed with PnP Powershell. Something like this:

Import-Module 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll'

$userName = "username"
$site = "siteUrl"
$pwd = Read-Host -Prompt "Please enter your password" -AsSecureString
$context = New-Object Microsoft.SharePoint.Client.ClientContext($site)
$cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName,$pwd)
$context.Credentials = $cred

# disabling "Allow members to share the site and individual files and folders"
$web = $context.Web
$context.Load($web)
$context.ExecuteQuery()
$web.MembersCanShare = $false
$web.Update()

# disabling "Allow members to invite others to the site members group"
$membersgroup = $context.Web.AssociatedMemberGroup
$context.Load($membersgroup)
$context.ExecuteQuery()
$membersgroup.AllowMembersEditMembership = $false
$membersgroup.Update()
$context.ExecuteQuery()

How can I combine the two approaches? What do you suggest?

Était-ce utile?

La solution

It would be, in my opinion, a good idea to only use CSOM code in PowerShell instead of combining both CSOM and PnP. As you mentioned, PnP PowerShell doesn't support the above methods, you can try the below code. Your PnP PowerShell command (Get-PnPSubWebs - Recurse) internally works in a similar manner :

# Recursive function to iterate all sites and subsites in all levels
function RecursiveWebs($ctx, $web, $l){
    $l++;
    $start = "";
    for ($i = 0 ; $i -le $l ; $i++){
        $start += "  ";
    }

    $counter = 1;
    foreach($web in $web.Webs)
    {
        $url = $start + $l + "." + $counter + ": " + $web.Url;      

        $web = $ctx.Web
        $ctx.Load($web)
        $ctx.ExecuteQuery()
        $web.MembersCanShare = $false
        $web.Update()
        Write-host "disabled Allow members to share the site and individual files and folders"


        $membersgroup = $ctx.Web.AssociatedMemberGroup
        $ctx.Load($membersgroup)
        $ctx.ExecuteQuery()
        $membersgroup.AllowMembersEditMembership = $false
        $membersgroup.Update()
        $ctx.ExecuteQuery()
        Write-Host "disabling Allow members to invite others to the site members group"

        Write-Host $url -ForegroundColor Green;
        $ctx.Load($web.Webs);
        $ctx.ExecuteQuery();
        if ($web.Webs.Count -gt 0){         
            RecursiveWebs $ctx $web $l
        }
        $counter++
    }
}



$tenantAdmin = "username"
$tenantAdminPassword = "password"
$siteUrl = "https://tenant.sharepoint.com/sites/test"
$secureAdminPassword = $(convertto-securestring $tenantAdminPassword -asplaintext -force)

# Main 
try
{
    Write-Host "Loading Dlls" -ForegroundColor Blue

    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"

    Write-Host "  - Done" -ForegroundColor Green
    Write-Host "Connecting SPO" -ForegroundColor Blue
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl) 
    $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($tenantAdmin, $secureAdminPassword)  
    $ctx.Credentials = $credentials
    $ctx.AuthenticationMode = [Microsoft.SharePoint.Client.ClientAuthenticationMode]::Default
    Write-Host "  - Done" -ForegroundColor Green
    $level = 1;
    $web = $ctx.Web
    $ctx.Load($web)
    $ctx.ExecuteQuery()

    Write-Host "Root Site"
    $url = "1:" + $web.Url
    Write-Host $url -ForegroundColor Green
    $ctx.Load($web.Webs)
    $ctx.ExecuteQuery()
    if ($web.Webs.Count -gt 0){

        RecursiveWebs $ctx $web $level
    }
}
catch 
{
    Write-Host "`n[Main] Errors found:`n$_" -ForegroundColor Red
}

Referenced and modified from this excellent blog

Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top