0

我现在正在接触 PowerShell 中的脚本世界,我需要你的帮助来编写一个可能微不足道的小脚本。

我正在尝试使用以下请求制作脚本:

  • 通过输入各种邮箱/共享邮箱(用户所在的邮箱)
  • 删除用户自动挂载(在这种情况下,它被发现为“test@test.com”。

脚本暂时是这样的:

#populate the list with the Mailbox / Shared to be removed
$mailboxList = Get-Content -Path "C:\Temp\Mailboxlist.txt"
$ConfirmPreference = 'none'

foreach($Mailbox in $mailboxlist)
{
        #perform the operation on the current $ mailbox
        Get-mailboxpermission -identity $Mailbox -User "test@test.com" | ? {$.AccessRights -like "FullAccess" -and $.IsInherited -eq $false } | remove-mailboxpermission -confirm:$false

 Add-MailboxPermission -Identity $Mailbox -User "test@test.com" -AccessRights:FullAccess -AutoMapping $false

}

我想知道这个命令是否可以正常工作,也是因为每次我尝试它都会出现:

PS C:\Temp> C:\Temp\RemoveAutoMapping.ps1
The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties 
do not match any of the parameters that take pipeline input.
    + CategoryInfo          : InvalidArgument: (Microsoft.Excha...sentationObject:PSObject) [Remove-MailboxPermission], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.Exchange.Management.RecipientTasks.RemoveMailboxPermission
    + PSComputerName        : outlook.office365.com

示例截图

在此先感谢大家。

亚历克斯

4

1 回答 1

0

你也许可以试试这个:

function Remove-Automap {
    [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" }
    } #foreach (removing)

    foreach ($mailbox in $address) {
        Add-MailboxPermission `
            -Identity $mailbox `
            -AccessRights FullAccess `
            -InheritanceType All `
            -AutoMapping:$false `
            -User $user

        Add-RecipientPermission -Identity "$mailbox" -AccessRights SendAs -Trustee "$user" -Confirm:$false
        Set-Mailbox $mailbox -GrantSendOnBehalfTo @{add = "$user" }
        
    } #foreach (adding)
} #function Remove-Automap

由于这些函数使用相同的参数,您可以删除并在相同的函数中轻松读取它。显然,根据需要更改访问权限和/或其他权限,例如 SendAs。

于 2022-03-01T00:12:52.510 回答