2

I am working on my Office 365 Exchange instance with Powershell and am having trouble with a command that I know I've run successfully in the past. I have broken this command down into its sub pieces and run them all fine on their own but just cannot seem to make this ForEach loop work. What might I be missing here?

PS C:\Users\bsigrist> ForEach ($Mailbox in (Get-Mailbox -RecipientTypeDetails UserMailbox)) 
{ $cal = $Mailbox.alias+":\Calendar" Set-MailboxFolderPermission -Identity $cal 
  -User Default -AccessRights LimitedDetails }

        At line:1 char:108
        + ... cal = $Mailbox.alias+":\Calendar" Set-MailboxFolderPermission -Identi ...
        +                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~
        Unexpected token 'Set-MailboxFolderPermission' in expression or statement.
            + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordExcep
           tion
            + FullyQualifiedErrorId : UnexpectedToken
bsigrist
  • 235
  • 1
  • 4
  • 9

2 Answers2

3

$cal = $Mailbox.alias+":\Calendar" Set-MailboxFolderPermission -Identity $cal -User Default -AccessRights LimitedDetails

That is being sent as one Powershell command, but I think you actually want it to be two commands. The first command assigns a value to $cal and the second command runs Set-MailboxFolderPermission.

As longneck points out, you can put a semicolon to separate those commands. Also discussed in the following locations:

https://blogs.msdn.microsoft.com/mattn/2012/04/28/powershell-command-separator-is-and-not/

https://superuser.com/questions/612409/how-do-i-run-multiple-commands-on-one-line-in-powershell

Todd Wilcox
  • 2,851
  • 2
  • 20
  • 32
3

You're missing a semi-colon after ":\Calendar"

longneck
  • 23,082
  • 4
  • 52
  • 86