Setting permissions on Outlook subfolders without access to inbox

Hello, I’m a very basic PS user. I just started teaching it to myself last year. The script below is supposed to add the user to every folder below Inbox\Case Files and make them the owner. I built it last year after searching online for days for help. I already gave them FolderVisible access to the Inbox and Case Files. I used this last year and it worked. Now when I try to run it on a new hire, it doesn’t do anything. And when I turned on Verbose, it looks like it tries to run but doesn’t actually change anything. I can provide that if someone wants to see it. I’ve removed the domain below but otherwise the script is intact. Can someone please take a look and tell me where it’s going wrong?

ForEach($f in (Get-EXOMailboxFolderStatistics -identity jjo | Where { $_.FolderPath.Contains(“jjo:/Inbox/Case Files”) -eq $True } ) ) {
$fname = “jjo:” + $f.FolderPath.Replace(“/”,"");
Add-MailboxFolderPermission $fname -User gka -AccessRights Owner
Write-Host $fname
Start-Sleep -Milliseconds 1000
}

Get-EXOMailboxFolderStatistics -identity jjo -ErrorAction Stop | Where-Object { $.FolderPath.Contains(“jjo:/Inbox/Case Files”) } | ForEach-Object {
Try {
$fname = “jjo:” + $
.FolderPath.Replace(“/”,"")
Write-Host “Amending: $fname …”
Add-MailboxFolderPermission $fname -User gka -AccessRights Owner -ErrorAction Stop
Write-Host “Done”
}
Catch {
$_
}
}
Write-Host “Complete”

Helps if you format your code with the ‘</>’ button. Do you get output or is it returning nothing?

It gives me the Complete at the end but that’s it. Last year when I used it, it listed out each folder as it changed it. I don’t know what’s changed.

So do you get output from just Get-EXOMailboxFolderStatistics -identity jjo

Yes, it gives me tons of pages of info on the folders. Too many to even show in Powershell.

Formatting your script as code on the forum is pretty much a requirement for peer review. Since you still haven’t edited your original post to format the code I decided to do it so I could read it:

ForEach($f in (Get-EXOMailboxFolderStatistics -identity jjo | Where-Object { $_.FolderPath.Contains("jjo:/Inbox/Case Files") -eq $True })) {
        $fname = "jjo:" + $f.FolderPath.Replace("/","");
        Add-MailboxFolderPermission $fname -User gka -AccessRights Owner
        Write-Host $fname
        Start-Sleep -Milliseconds 1000
    }
    
Get-EXOMailboxFolderStatistics -identity jjo -ErrorAction Stop | Where-Object { $.FolderPath.Contains("jjo:/Inbox/Case Files") } | ForEach-Object {
    Try {
        $fname = "jjo:" + $.FolderPath.Replace("/","")
        Write-Host "Amending: $fname …"
        Add-MailboxFolderPermission $fname -User gka -AccessRights Owner -ErrorAction Stop
        Write-Host "Done"
    } Catch {
        $_
    }
}
Write-Host "Complete"

And then I was able to see an issue. On line 8 after you pipe Ge-tEXOMailboxFolderStatistics to Where-Object there’s a missing underscore on the automatic variable for “Current object”.
This:

Get-EXOMailboxFolderStatistics -identity jjo -ErrorAction Stop | Where-Object { $.FolderPath.Contains("jjo:/Inbox/Case Files") } | ForEach-Object {...

Should be this:

Get-EXOMailboxFolderStatistics -identity jjo -ErrorAction Stop | Where-Object { $_.FolderPath.Contains("jjo:/Inbox/Case Files") } | ForEach-Object {...

This would cause the Where-Object statement to return nothing, and so the Foreach-Object loop afterwards would not iterate through anything.
And again on line 10, a missing underscore:

$fname = "jjo:" + $.FolderPath.Replace("/","")

These two loops are trying to accomplish the same action right? It looks like two different ways of applying the permissions. This was ran as a script?

1 Like

I believe the missing underscores is caused by not formatting the code and the forum strips them.

1 Like

ah ok that makes sense, then i suppose my last thought about why the script is doing the operation twice still stands.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.