用于从分发列表中删除用户的Powershell脚本

时间:2019-05-09 21:33:23

标签: powershell-v2.0

自从收到通知以来,我不再要求编写代码,因此可以修改发布内容。 我有这个代码。它可以运行,但不会将任何数据拉入cvs文件或实际上从DL中删除用户。不知道为什么它不起作用。这是在带有交换管理控制台的本地交换服务器2010上运行的。我很确定这是我所困扰的地方,但不确定如何纠正它。 where-object {($ _。identity -eq“ o365.test3”)}

 Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 
#path to output file
$OutFile = 'C:\Scripts\Logs\' 
$OutFile_LOG = $OutFile+'DL_removeusers_'+$(get-date).ToString('yyyyMMdd')+'.csv'

$totalObj = @()
#get all distribution groups
$temp = Get-DistributionGroup -ResultSize Unlimited | 
ForEach-Object { 
  #get certain user from Distribution Lists
  [array]$mem = Get-DistributionGroupMember -id $_ -ResultSize Unlimited | where-object {($_.identity -eq "o365.test3") } 
  for ($i = 0; $i -lt $mem.Count; $i++) { 
    $member = $mem[$i].name 

    #remove user from distribution group
    Remove-DistributionGroupMember -Identity $_ -Member $mem[$i].DistinguishedName -BypassSecurityGroupManagerCheck -confirm:$false     
    $obj = New-Object System.Object 
    $obj | Add-Member -MemberType NoteProperty -Value $_.Name -Name 'Distribution Group' -Force 
    $obj | Add-Member -MemberType NoteProperty -Value $member -Name 'Members' -Force 
    $obj | Add-Member -MemberType NoteProperty -Value $mem[$i].SamAccountName -Name 'SamAccountName' -Force 
    $obj | Add-Member -MemberType NoteProperty -Value $mem[$i].identity -Name 'OU' -Force -PassThru 
    $totalObj += $obj 
  } 
}

#create output file
$totalObj | Export-Csv $OutFile_LOG
Read-Host -Prompt “Press Enter to exit” 

0 个答案:

没有答案