AD Remove-DistributionGroupMembers无法正常工作

时间:2015-03-14 04:51:49

标签: powershell active-directory-group

我有下面的脚本。最后一部分是删除不起作用的组成员。我可以在Active Directory中创建和更新组。

#import AD module
Import-Module activedirectory
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://rz1exccas1v.bluet.lan/PowerShell/ -Authentication Kerberos
Import-PSSession $Session
$DATE = $(get-date -f yyyy-MM-dd)
$csv = Import-Csv E:\myfolder\Groups\new-dynamicgroup-department\users.csv -Header @("Company","Office","Department","MailGroup")
$EXPORTFILE = "E:\myfolder\Groups\new-dynamicgroup-department\changes\$DATE-CreateMailGroups.txt"
$CurrentOutput =""

foreach ($line in $csv) {
                        #Variables
                        $Company = $line.Company
                        $Office = $line.Office
                        $Department = $line.Department
                        $MailGroup = $line.MailGroup

                        Write-Host "Company = $Company"
                        Write-Host "Office = $Office"
                        Write-Host "Department= $Department"
                        Write-Host "MailGroup= $MailGroup"

#Update Mailaddress
if ($Company -eq "Gokulsoft Argentina S.R.L.") {$ExternalGroupMail = "$MailGroup@rohlig-procargo.com"} elseif (($Company -eq " Gokulsoft CANADA INC.") -or ($Company -eq " Gokulsoft INDIA PRIVATE LIMITED") -or ($Company -eq " Gokulsoft SINGAPORE PTE. LTD.") -or ($Company -eq " Gokulsoft USA LLC.") -or ($Company -eq " Gokulsoft HONG KONG LTD.") -or ($Company -eq " Gokulsoft Int'l Forwarding (Shenzhen) Co. Ltd.") )  {$ExternalGroupMail = "$MailGroup@gokulsoft.com"} elseif ($Company -eq "R+C COMMODITY BMGH & CO. KG"){$ExternalGroupMail = "$MailGroup@rc-commodity.com"} else  {$ExternalGroupMail = "$MailGroup@gokulsoft.com"}

                                                                     cd
Try
{
$exists = Get-ADGroup $MailGroup                                                                           
}
#create group if necessary - and define external group mailaddress
Catch
{
$CurrentOutput = "Group $MailGroup does not exist. Creating it."
Write-Host $CurrentOutput
out-file -filepath $EXPORTFILE -inputobject $CurrentOutput -encoding UTF8 -append
New-DistributionGroup -Name $MailGroup -OrganizationalUnit "bluet.lan/Company/Groups - Mail Groups (autofill)" -SamAccountName $MailGroup -Type "Security"
Set-DistributionGroup -Identity  $MailGroup   -EmailAddresses "smtp:$ExternalGroupMail" -EmailAddressPolicyEnabled:$false -CustomAttribute1 "UsermanagementGroup" -CustomAttribute2 "last modified $DATE " -ManagedBy "agc.admin@gokulsoft.com" -RequireSenderAuthenticationEnabled $False
                            }
#Remove GroupMembers
$CurrentOutput = "Group $MailGroup exists. Removing Members"
Write-Host $CurrentOutput
 out-file -filepath $EXPORTFILE -inputobject $CurrentOutput -encoding UTF8 –append
Set-Distributiongroup –identity $MailGroup -ManagedBy "fabrikam.admin@gokulsoft.com","contoso.admin@gokulsoft.com","agc.admin@gokulsoft.com"
$CurrentOutput = "Group $MailGroup - external Mailaddress : $ExternalGroupMail"
Write-Host $CurrentOutput
out-file -filepath $EXPORTFILE -inputobject $CurrentOutput -encoding UTF8 –append
$GroupMembersDn = (Get-DistributionGroupMember -Identity $MailGroup).DistinguishedName
ForEach ($item in $GroupMembersDn) {Remove-DistributionGroupMember -Identity $MailGroup -Member $item -Confirm:$False}
Remove-PSSession $Session

0 个答案:

没有答案