Set-MailBoxFolderPermission fails in script

So I’ve got a script that’s supposed to automatically create an Exchange inbox and create a few settings. It works alright, except for one part, where I attempt to run the following command:

Set-MailboxFolderPermission -User "Default" -AccessRights "LimitedDetails" -Identity username:Calendar 

instead of “($UsersSamAccountName):Calendar” I use the users SamAccountName directly followed by :Calendar and this usually works.

Now, the weird part I dont understand. If I run that line as a standalone command it runs perfectly fine. If it’s run in the script I get the following error

Cannot open mailbox /o=company/ou=Exchange Administrative Group (XXXXXXXXXXX)/cn=Configuration/cn=Servers/cn=servername/cn=Microsoft System Attendant. + CategoryInfo : NotSpecified: (0:Int32) [Set-MailboxFolderPermission], ObjectNotFoundException + FullyQualifiedErrorId : F5963A48,Microsoft.Exchange.Management.StoreTasks.SetMailboxFolderPermission + PSComputerName : exchange fqdn

If I immidiately run the command as a stand alone command now, I get this error:

Set-MailboxFolderPermission -User "Default" -AccessRights "LimitedDetails" -Identity username:Calendar 

Cannot open mailbox /o=company/ou=Exchange Administrative Group (XXXXXXXXXXX)/cn=Configuration/cn=Servers/cn=servername/cn=Microsoft System Attendant. + CategoryInfo : NotSpecified: (0:Int32) [Set-MailboxFolderPermission], ObjectNotFoundException + FullyQualifiedErrorId : FA7C611B,Microsoft.Exchange.Management.StoreTasks.SetMailboxFolderPermission + PSComputerName : exchange fqdn

So it fails again. But If I wait 5-10 minutes and run the exact same command (I literally just wait and press arrow up to go to the lastes command) I get this:

PS C:ToolsPSscripts> Set-MailboxFolderPermission -User "Default" -AccessRights "LimitedDetails" -Identity username:Calendar PS C:ToolsPSscripts> 

It has completed successfully.

This is some dark magic I cannot understand, i I really don’t know what to google. Any help would be appricated!

submitted by /u/TigerBone
[link] [comments]

Leave a Reply