Powershell

Powershell Exchange命令循環不起作用

  • November 14, 2017

我正在使用 Powershell 處理我的 Office 365 Exchange 實例,並且我知道我過去成功執行的命令遇到了問題。我已將此命令分解為其子部分並自行執行它們,但似乎無法使此 ForEach 循環正常工作。我可能在這裡缺少什麼?

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

您在之後缺少一個分號":\Calendar"

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

這是作為一個 Powershell 命令發送的,但我認為您實際上希望它是兩個命令。第一個命令為 分配一個值,$cal第二個命令執行Set-MailboxFolderPermission

正如 longneck 指出的那樣,您可以用分號分隔這些命令。還在以下位置進行了討論:

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

引用自:https://serverfault.com/questions/883401