【问题标题】:PowerShell Script | Remove auto-map for single user on many mailboxesPowerShell 脚本 |删除多个邮箱上单个用户的自动映射
【发布时间】:2022-03-01 08:14:41
【问题描述】:

我现在正在接触 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

Example Screenshot

提前感谢大家。

亚历克斯

【问题讨论】:

标签: azure powershell exchange-server script automount


【解决方案1】:

你可以试试这个:

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。

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
最近更新 更多