Forum Discussion
PowerShell Sharepoint Mnage Access Issue
Hi everyone,
I'm working on a PowerShell script that navigates through a SharePoint document library and allows users to create a "Job Number" folder with a set of predefined subfolders. Everything's working fine so far, but I'm stuck on how to configure permissions.
I need to set up the OffersRW group with read-only access to all folders in the document library, except for the BOM folder. Specifically, OffersRW should have read/write access to BOM but only read access to everything else.
Does anyone have experience with this type of permission setup in SharePoint? Any guidance on how to implement this through PowerShell or SharePoint UI would be greatly appreciated!
Thanks in advance!
$siteUrl = "XX"
$documentLibrary = "Folders"
Connect-PnPOnline -Url $siteUrl -UseWebLogin
function ShowOptions {
param (
[string]$currentPath
)
Write-Host "Fetching folders from: $currentPath"
$folder = Get-PnPFolder -Url $currentPath -Includes Folders
$folder.Context.Load($folder.Folders)
$folder.Context.ExecuteQuery()
$folders = $folder.Folders
Write-Host "Found $($folders.Count) folders"
# Always show "Create Job Number" as the first option
Write-Host "1. Create Job Number"
$index = 2
foreach ($folder in $folders) {
Write-Host "$index. $($folder.Name)"
$index++
}
return $folders
}
$currentPath = $documentLibrary
$exit = $false
while (-not $exit) {
try {
$folders = ShowOptions -currentPath $currentPath
} catch {
Write-Host "Error fetching folders: $_"
break
}
$choice = Read-Host "Enter your choice"
if ($choice -eq 1) {
$folderNumber = Read-Host "Enter the folder number"
$mainFolder = "$folderNumber"
$subfolders = @("Drawing", "BOM", "Offers", "Ehs Docs", "Job Order", "Cost Up")
# Check if the main folder already exists
$existingFolder = Get-PnPFolder -Url "$currentPath/$mainFolder" -ErrorAction SilentlyContinue
if ($existingFolder) {
Write-Host "Error: Folder $mainFolder already exists inside $currentPath"
break
}
# Create the main folder inside the current path
Add-PnPFolder -Name $mainFolder -Folder $currentPath
Write-Host "Created folder: $mainFolder inside $currentPath"
# Loop through each subfolder and create them
foreach ($subfolder in $subfolders) {
Add-PnPFolder -Name $subfolder -Folder "$currentPath/$mainFolder"
Write-Host "Created subfolder: $subfolder in $mainFolder inside $currentPath"
}
Write-Host "Folders created inside $currentPath successfully!"
} else {
$selectedFolder = $folders[$choice - 2] # Adjusted index to select the correct folder
$currentPath = "$currentPath/$($selectedFolder.Name)"
}
}