Powershell 从多个用户中删除多个共享邮箱

Powershell remove multiple shared mailboxes from multiple users

现在我有一个功能可以删除单个用户的多个共享电子邮件。但是,我希望能够指定多个用户,这些用户也需要从他们的帐户中删除多个共享电子邮件。我想不出一个好的方法来做到这一点?

这是我目前所拥有的并且可以正常工作,但我只能在 -user 参数上指定一个用户。

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string[]]$address,

        [Parameter(Mandatory = $true)]
        [string]$user
    )
    foreach ($mailbox in $address) {
        Remove-MailboxPermission `
            -Identity $mailbox `
            -AccessRights FullAccess `
            -Confirm:$false `
            -User $user

        Remove-RecipientPermission `
            -Identity $mailbox `
            -AccessRights SendAs `
            -Confirm:$false `
            -Trustee $user

        Set-Mailbox $mailbox -GrantSendOnBehalfTo @{remove = "$user" }
    }
} #function Remove-SharedMailbox```

继续我的评论:

function Remove-SharedMailbox {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string[]]$address,

        [Parameter(Mandatory = $true)]
        [string[]]$user
    )
    foreach ($mailbox in $address) 
    {
        foreach ($account in $user)
        {
            Remove-MailboxPermission `
                -Identity $mailbox `
                -AccessRights FullAccess `
                -Confirm:$false `
                -User $account

            Remove-RecipientPermission `
                -Identity $mailbox `
                -AccessRights SendAs `
                -Confirm:$false `
                -Trustee $account

            Set-Mailbox $mailbox -GrantSendOnBehalfTo @{remove = $account }
        }
    }
}

它最终与您已经在嵌套循环中使用的逻辑相同。刚刚通过将 [string[]] 强制转换为 $user 来接受 strings 的数组。嵌套循环使用 $account.

遍历传递给它的用户