Powershell O365 嵌套 DL - 添加 AcceptMessagesOnly 来自

Powershell O365 嵌套 DL - 添加 AcceptMessagesOnly 来自

目前,我们在 Powershell 中有一个简短的脚本,可以修改嵌套的 DL。它们目前采用顶部 DL 并将用户添加到 AcceptOnlyMessagesFrom 部分。以前只有一层嵌套,现在它们已被修改,因此它们嵌套了多次。我该如何修改此脚本以使其循环下去,使其一直向下?

$DistributionList = "MainDistributionList"
$Members = "Email1","Email2"
$DLs = Get-DistributionGroupMember $DistributionList | Select-Object DisplayName
Set-DistributionGroup -Identity $DistributionList -AcceptMessagesOnlyFrom @{Add=$Members}
ForEach ($DL in $DLs) {Get-DistributionGroup -Identity $DL.DisplayName | Set-DistributionGroup -AcceptMessagesOnlyFrom @{Add=$Members}}

编辑

所以我设法想出了一个方法,但并不是我想要的,因为它不是一个合适的循环,我只是手动把它们全部写出来,看看我们有多少个嵌套级别。不过现在这样就行了,而且比手动操作要容易/快得多。教我吧,Powershell 专家!

$DistributionList = "ParentDL"
$Members = "Email1","Email2"
Set-DistributionGroup -Identity $DistributionList -AcceptMessagesOnlyFrom @{Add=$Members}
$Nested1DLs = Get-DistributionGroupMember $DistributionList | Select-Object DisplayName
$Nested2DLs = Foreach ($DL in $Nested1DLs) {Get-DistributionGroupMember -Identity $DL.DisplayName | Select-Object DisplayName,RecipientType | Where-Object {$_.RecipientType -like "MailUniversalDistributionGroup"}}
$Nested3DLs = Foreach ($DL in $Nested2DLs) {Get-DistributionGroupMember -Identity $DL.DisplayName | Select-Object DisplayName,RecipientType | Where-Object {$_.RecipientType -like "MailUniversalDistributionGroup"}}
ForEach ($DL in $Nested1DLs) {Get-DistributionGroup -Identity $DL.DisplayName | Set-DistributionGroup -AcceptMessagesOnlyFrom @{Add=$Members}}
ForEach ($DL in $Nested2DLs) {Get-DistributionGroup -Identity $DL.DisplayName | Set-DistributionGroup -AcceptMessagesOnlyFrom @{Add=$Members}}
ForEach ($DL in $Nested3DLs) {Get-DistributionGroup -Identity $DL.DisplayName | Set-DistributionGroup -AcceptMessagesOnlyFrom @{Add=$Members}}

答案1

尝试一下这个脚本。前 3 个变量供您设置:

变量

  1. $WhatIf:在进行任何更改之前查看脚本将要执行的操作。设置为$false当您准备好进行这些更改时。
  2. $TopGroup:包含嵌套组的最顶层组。AcceptMessagesOnlyFrom源自驻留在此处的直接成员。
  3. $OverwriteAcceptMessagesOnlyFrom:如果$trueAcceptMessagesOnlyFrom将被覆盖;如果,它将被附加到(参见脚本输出)。如果您有想要保留的现有用户或计划在将来手动添加一些用户,$false则应将其设置为。$false

脚本

$WhatIf = $true
$TopGroup = "[email protected]"
$OverwriteAcceptMessagesOnlyFrom = $true

function Get-DistributionGroupRecursive {
  Param (
    [Parameter(Mandatory=$true, Position=0)][string[]]$Identity,
    [Parameter(Mandatory=$false, DontShow)][string[]]$LastResults,
    [Parameter(Mandatory=$false, DontShow)][switch]$ChildSearch = $false
  )

  $Condition = { $_.RecipientType -eq "MailUniversalDistributionGroup" -and $LastResults -notcontains $_ }
  $Identity | ForEach-Object {
    if ($ChildSearch) {
      $Results = Get-DistributionGroupMember -Identity $_ -ResultSize Unlimited | Where-Object $Condition | Get-DistributionGroup
    } else {
      $Results = Get-DistributionGroup -Identity $_ -ResultSize Unlimited | Where-Object $Condition
    }
    if (($Results | Measure-Object).Count -gt 0) {
      $Results
      Get-DistributionGroupRecursive -Identity $Results.Name -LastResults ($LastResults + $Results.Name) -ChildSearch
    }
  }
}

$TargetGroups = Get-DistributionGroupRecursive $TopGroup
Write-Host "Target distribution groups will be:"
$TargetGroups | ForEach-Object {
  Write-Host "  - $($_.PrimarySmtpAddress)"
}

$TopGroupMembers = Get-DistributionGroupMember -Identity $TopGroup | Where-Object { $_.RecipientType -eq "UserMailbox" }
Write-Host "`r`nAcceptMessagesOnlyFrom will be:"
$TopGroupMembers | ForEach-Object {
  Write-Host "  - $($_.PrimarySmtpAddress)"
}

if ($OverwriteAcceptMessagesOnlyFrom) {
  Write-Host "`r`nAcceptMessagesOnlyFrom will be overwritten. Any existing or manually added entries will be deleted!`r`n"
  $TargetGroups | Set-DistributionGroup -AcceptMessagesOnlyFrom $TopGroupMembers.Name -WhatIf:$WhatIf
} else {
  Write-Host "`r`nAcceptMessagesOnlyFrom will be appended to. Members removed from top group in the future will need to be manually removed from nested groups' AcceptMessagesOnlyFrom!`r`n"
  $TargetGroups | Set-DistributionGroup -AcceptMessagesOnlyFrom @{ Add = $TopGroupMembers.Name } -WhatIf:$WhatIf
}

示例输出

在此示例运行中,我们有一些组嵌套在其他组内。topdog1topdog2dltest1,这样他们就成为能够向所有目标群体发送电子邮件的独家用户。

Target distribution groups will be:
  - [email protected]
  - [email protected]
  - [email protected]
  - [email protected]
  - [email protected]

AcceptMessagesOnlyFrom will be:
  - [email protected]
  - [email protected]

AcceptMessagesOnlyFrom will be overwritten. Any existing or manually added entries will be deleted!

What if: Setting distribution group Identity:"DLTest120210618024157".
What if: Setting distribution group Identity:"DLTest220210618024235".
What if: Setting distribution group Identity:"DLTest320210618024254".
What if: Setting distribution group Identity:"DLTest420210618055828".
What if: Setting distribution group Identity:"DLTest520210621111355".

相关内容