2

I have written a script to create folders in SPO, this script creates a group, then a folder and assigns the group permission to the folder. The only issue is that the folder is created by the user running the script, so the folder has permissions for the creating user & the group.

After creating the folder and assigning permissions, I want to go back and remove any users that have permission on that folder (excluding the group i created).

I've seen quite a few guides on how to do this but I'm getting stuck when debugging as things aren't initializing correctly.

Take for instance this guide.

It starts by getting the site context and using the web property to handle all of the permissions. Up to this line my code debugs fine:

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Site)
$ctx.Credentials = $creds

The next step is to run the following:

$Web = $Ctx.Web

#Get the Folder
$Folder = $Web.GetFolderByServerRelativeUrl($folderurl)
$Ctx.Load($Folder)
$Ctx.ExecuteQuery()

However when inspecting values in debug, $Web is completely empty and never gets assigned. Here is my context object initialised:

Context initialised

and here is the $Web variable (after attempting to set it):

Web variable

As you can see this variable has not been set even though it exists in the context variable. How can I fix this issue? I've not seen much about it and I'm at a loss on how I can actually connect to sharepoint and remove the permissions now!

0

Based on your screenshots, you are debugging the PowerShell commands.

It is inaccurate. It will not show any value for $Web. You can output the $Web.Title, and you can find it has values.

enter image description here

Per my research, it is by designed. The current user running the commands will automatically be assigned Full Control when breaking the permission inheritance and removing permissions with BreakRoleInheritance($False,$False). It ensures that the current user has access to manage this item/folder.

  • Thanks for the answer. I checked and $web does have a title when running through, thats good (horrendous for debugging, but hey it's powershell). I understand why my user gets assigned, but I need to break permissions, add 1 group and remove everyone else. My $Web object still never returns anything , for instance I try to build the folder url up dynamically with $folderurl = $Web.ServerRelativeUrl + "/" + $ListName + "/" + $RootFolder. Nothing gets returned here.... I'm going to look into doing this through c#, I don't know why I didn't in the first place! – Axemasta Nov 21 '18 at 10:58
  • I have tested c#. It is same. The current user running the command will be added to it. You can output $Web.ServerRelativeUrl for checking. You will not get any value when outputting $Web. – JoannaW_MSFT Nov 22 '18 at 1:33

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.