Share via

Problems with .update() method to change conditional access policy for SPO site using PNP.

TSERKOVNYUK, Oleg 21 Reputation points
2021-02-04T14:28:56.653+00:00

Hello,

I am trying o change conditional access policy for SharePoint Online site using PNP.
I did this previously using code:

$url = 'https://companygroup.sharepoint.com/sites/TeamBlockPolicy'
Connect-pnponline $url -useweblogin
$context = Get-PnPContext
$site = Get-PnPTenantSite -Url $url
$site.ConditionalAccessPolicy = [Microsoft.Online.SharePoint.TenantManagement.SPOConditionalAccessPolicyType]::BlockAccess
$site.Update()
Invoke-PnPQuery

Code above used to work in August, but not working anymore. When I call $site.Update() I get error:
'Method invocation failed because [PnP.PowerShell.Commands.Model.SPOSite] does not contain a method named 'Update'.'
Command "$site | gm" does not show method 'Update'

PNP version: SharePointPnPPowerShellOnline 3.29.2101.0
PowerShell version: 5.1

Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

ZhengyuGuo 10,591 Reputation points Moderator
2021-02-05T06:33:43.843+00:00

Hi @TSERKOVNYUK, Oleg ,

I tested the script with the new version "PnP.PowerShell" and old "SharePointPnPPowerShellOnline 3.29.2101.0" installed in local, the script will throw the same error (Only SharePointPnPPowerShellOnline 3.29.2101.0" installed won't throw the error):

64436-snipaste-2021-02-05-14-00-47.png

For this issue, I suggest you can verify if the new PnP.PowerShell installed in your local using this command:

Get-Module -Name PnP.PowerShell -ListAvailable   

If it listed the version details like this, this mean the "PnP.PowerShell" installed:

64441-snipaste-2021-02-05-14-26-55.png

Then the error is because of new version "PnP.PowerShell" has removed Update function for [PnP.PowerShell.Commands.Model.SPOSite] .

A solution is specify which module to use using Import-Module:

Import-Module -Name SharePointPnPPowerShellOnline  
  
$url = 'https://companygroup.sharepoint.com/sites/TeamBlockPolicy'  
Connect-pnponline $url -useweblogin  
$context = Get-PnPContext  
$site = Get-PnPTenantSite -Url $url  
$site.ConditionalAccessPolicy = [Microsoft.Online.SharePoint.TenantManagement.SPOConditionalAccessPolicyType]::BlockAccess  
$site.Update()  
Invoke-PnPQuery  

The error message will gone:

64413-snipaste-2021-02-05-14-30-13.png

Thanks
Best Regards


If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Kjetil Ytrehus-Lynum 6 Reputation points
    2022-03-08T18:53:56.477+00:00

    Set-PnpTenantSite -Identity $siteUrl -DenyAddAndCustomizePages:$false

    This works. :) Im using PnP.Powershell 1.9.0 in Visual Studio Code right now.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

  2. TSERKOVNYUK, Oleg 21 Reputation points
    2021-02-05T10:07:28.353+00:00

    Thank you for solution. That resolved my problem. Interesting how the same result should be achieved in new version of PNP, because SharePointPnPPowerShellOnline is legacy.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.