0

We are using the following script due to the SharePoint REST API not controlling these properties. When I run this below script as a whole, I get:

Exception calling "ExecuteQuery" with "0" argument(s): "The web being updated was changed by an external process."
At line:41 char:1
+ $web.Context.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ServerException

If I re-run the last 4 lines it works though:

$web.MembersCanShare = $false
$web.SetUseAccessRequestDefaultAndUpdate($true)
$web.Update()
$web.Context.ExecuteQuery()

Entire script is below:

$varURL = Read-Host -Prompt 'Provide URL to site'
Connect-PnPOnline -Url $varURL
#Set all your variables
$web = Get-PnPWeb -Includes Title
$OwnersGroup = $web.Title.substring($web.Title.IndexOf(' | ')+3)+' Owners'
$MembersGroup = $web.Title.substring($web.Title.IndexOf(' | ')+3)+' Members'
$MembersEXTERNALGroup = $web.Title.substring($web.Title.IndexOf(' | ')+3)+' Members EXTERNAL'
$VisitorsGroup = $web.Title.substring($web.Title.IndexOf(' | ')+3)+' Visitors'
Set-PnPGroup -Identity $OwnersGroup -SetAssociatedGroup Owners
Set-PnPGroup -Identity $MembersGroup -SetAssociatedGroup Members
#Associate default Owners to Members/MembersExternal/Owners/Visitors Groups -Owner $OwnersGroup
#Turn off editing -AllowMembersEditMembership $false
#Let everyone see membership -OnlyAllowMembersViewMembership $false
Set-PnPGroup -Identity $MembersGroup -Owner $OwnersGroup
Set-PnPGroup -Identity $MembersGroup -AllowMembersEditMembership $false
Set-PnPGroup -Identity $MembersGroup -OnlyAllowMembersViewMembership $false
Set-PnPGroup -Identity $MembersEXTERNALGroup -Owner $OwnersGroup
Set-PnPGroup -Identity $MembersEXTERNALGroup -AllowMembersEditMembership $false
Set-PnPGroup -Identity $MembersEXTERNALGroup -OnlyAllowMembersViewMembership $false
Set-PnPGroup -Identity $OwnersGroup -Owner $OwnersGroup
Set-PnPGroup -Identity $OwnersGroup -AllowMembersEditMembership $false
Set-PnPGroup -Identity $OwnersGroup -OnlyAllowMembersViewMembership $false
Set-PnPGroup -Identity $VisitorsGroup -Owner $OwnersGroup
Set-PnPGroup -Identity $VisitorsGroup -AllowMembersEditMembership $false
Set-PnPGroup -Identity $VisitorsGroup -OnlyAllowMembersViewMembership $false
#Make sure members can't share nor edit member group
$web = Get-PnPWeb
#-Includes MembersCanShare, AssociatedMemberGroup.AllowMembersEditMembership
$web.MembersCanShare = $false
$web.SetUseAccessRequestDefaultAndUpdate($true)
$web.Update()
$web.Context.ExecuteQuery()
0

Try running $web.Context.ExecuteQuery() before 2nd $web = Get-PnPWeb in your code. complete script will be like below:

$varURL = Read-Host -Prompt 'Provide URL to site'
Connect-PnPOnline -Url $varURL
#Set all your variables
$web = Get-PnPWeb -Includes Title
$OwnersGroup = $web.Title.substring($web.Title.IndexOf(' | ')+3)+' Owners'
$MembersGroup = $web.Title.substring($web.Title.IndexOf(' | ')+3)+' Members'
$MembersEXTERNALGroup = $web.Title.substring($web.Title.IndexOf(' | ')+3)+' Members EXTERNAL'
$VisitorsGroup = $web.Title.substring($web.Title.IndexOf(' | ')+3)+' Visitors'
Set-PnPGroup -Identity $OwnersGroup -SetAssociatedGroup Owners
Set-PnPGroup -Identity $MembersGroup -SetAssociatedGroup Members
#Associate default Owners to Members/MembersExternal/Owners/Visitors Groups -Owner $OwnersGroup
#Turn off editing -AllowMembersEditMembership $false
#Let everyone see membership -OnlyAllowMembersViewMembership $false
Set-PnPGroup -Identity $MembersGroup -Owner $OwnersGroup
Set-PnPGroup -Identity $MembersGroup -AllowMembersEditMembership $false
Set-PnPGroup -Identity $MembersGroup -OnlyAllowMembersViewMembership $false
Set-PnPGroup -Identity $MembersEXTERNALGroup -Owner $OwnersGroup
Set-PnPGroup -Identity $MembersEXTERNALGroup -AllowMembersEditMembership $false
Set-PnPGroup -Identity $MembersEXTERNALGroup -OnlyAllowMembersViewMembership $false
Set-PnPGroup -Identity $OwnersGroup -Owner $OwnersGroup
Set-PnPGroup -Identity $OwnersGroup -AllowMembersEditMembership $false
Set-PnPGroup -Identity $OwnersGroup -OnlyAllowMembersViewMembership $false
Set-PnPGroup -Identity $VisitorsGroup -Owner $OwnersGroup
Set-PnPGroup -Identity $VisitorsGroup -AllowMembersEditMembership $false
Set-PnPGroup -Identity $VisitorsGroup -OnlyAllowMembersViewMembership $false
#Make sure members can't share nor edit member group

#I have added this line here
$web.Context.ExecuteQuery()

$web = Get-PnPWeb
#-Includes MembersCanShare, AssociatedMemberGroup.AllowMembersEditMembership
$web.MembersCanShare = $false
$web.SetUseAccessRequestDefaultAndUpdate($true)
$web.Update()
$web.Context.ExecuteQuery()
| improve this answer | |
  • Just tried, and get error due to last line #43: $web.Context.ExecuteQuery() Exception calling "ExecuteQuery" with "0" argument(s): "The web being updated was changed by an external process." At line:43 char:1 + $web.Context.ExecuteQuery() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ServerException – Sol Web Solutions Mar 6 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.