Set-Distributio nGroupOwners
This script is used to set the Distribution-Group Owners, this script is develop by-Microsoft.
#################################################################################
Param($DistributionGroup = $null,$GroupOwner = $null)
# Sets all users in the DL listed in $dn_storage as managing the DL they are listed in
Function SetUserAsOwners {
Param ([string]$DistributionGrouptoSet)
# Handle single DG vs Processing everything
if ($DistributionGrouptoSet -eq ""){
# Get a list of all groups that we need to manipulate
$Groupstoset = Get-distributiongroup -filter "($dn_storage -like 'CN*')"
}
# Set our Grouptoset to just the single group that was passed into the function
else { $Groupstoset = Get-distributiongroup $DistributionGrouptoSet }
# Process each group
$Groupstoset | foreach {
# Seting the array of users to null so that it doesn't keep adding to the array with each loop
[array]$DNOfUserstoset = $null
# Setting CheckedUserstoSet to Null to ensure we don't continue building the array over time
[array]$CheckedUserstoset = $null
Write-Host "Setting Members of" $_.($dn_storage) "as owners on" $_.identity
# Get a list of the users that need to be listed as managers of the DL
# This will preform this search recursively
$Userstoset = Get-ADGroupMember $_.($dn_storage) -recursive
# Convert the output from get-adgroupmember into an array of DNs
$Userstoset | foreach { [array]$DNOfUserstoset = $DNOfUserstoset + [string]$_.distinguishedname }
# Verify that each of the users in the array is a mailbox
# This eliminates any nested groups / contact / or users and just leaves us with the mailboxes
$DNOfUserstoset | foreach {
If (Get-mailbox $_ -erroraction silentlycontinue){[array]$CheckedUserstoset = $CheckedUserstoset + $_ }
else {}
}
# Throw any duplicates out of the $checkedUsersToSet
$CheckedUserstoset = $CheckedUserstoset | Select-Object -Unique
# Set that list of mailboxes as owners of the DL
# Throw a warning if we didn't get any valid objects returned
if ($CheckedUserstoset -eq $null){Write-warning "Didn't Find any valid objects in Owning Group"}
else { Set-distributiongroup $_.identity -managedby $CheckedUserstoset -BypassSecurityGroupManagerCheck }
}
}
# Setup a DL as "owner" of another DL
# This will place the DN of DistributionGroupOwner into the $dn_Storage file of the Distribtiongroup
Function SetDNofGroupOwner {
Write-Host "Setting" $GroupOwner "as the owner of" $DistributionGroup
# Build and Execute the command that we need to make this change
$commandtorun = "Set-Distributiongroup -identity `'" + $DistributionGroup + "`' -" + $dn_storage + " `(get-adgroup `'" + $GroupOwner + "`'`)`.distinguishedname"
Invoke-Expression $commandtorun
}
# Main Body
# ===============================
# Attribute to use for storing DN of group owner
# !!!! --- Change this to the correct attribute for your Organization --- !!!! #
$dn_storage = "CustomAttribute5"
#Check the OS Version
if ([system.environment]::OSversion.Version.Major -eq 6 -and [system.environment]::OSversion.Version.Minor -ge 1 -and (get-wmiobject Win32_OperatingSystem -comp .).Caption.Contains("R2") ) {}
else {
Write-Error "The Operating System requirements are not met, you must be running at least Windows 2008 R2"
exit
}
# Check to see if the Exchnage snapin is loaded, if not load it
if ((Get-PSSession | where {$_.configurationname -eq "Microsoft.Exchange"}) -eq $null) {
Write-Host "Loading Exchange Shell"
# Load up Exchange Powershell so we have the Exchange cmdlets
# !!!! --- You will need to change this Path if your Exchange Install is not in the Default Location --- !!!! #
. 'c:\Program Files\Microsoft\Exchange Server\v14\Bin\RemoteExchange.ps1'
Connect-ExchangeServer -auto
}
# Import the AD management Module
Import-Module ActiveDirectory
# If no parameters passed process all Distribution groups
If (($GroupOwner -eq $null) -and ($DistributionGroup -eq $null)){ SetUserAsOwners }
# If we have a Distributiongroup but not an owner then just process that DL
elseif (($GroupOwner -eq $null) -and ($DistributionGroup -ne $null)){ SetUserAsOwners -DistributionGrouptoSet $DistributionGroup }
# If we have DL owner and don't have a DL then we need to generate an error
elseif (($DistributionGroup -eq $null) -and ($GroupOwner -ne $null)) {Write-Error "Incorrect Syntax"}
# If none of the above then we should have both DL and DLOwner so set the DL owner attribute
else { SetDNofGroupOwner }
If you feel this is valuable, please leave your comments for me.
Thanks
Arun Chaudhary
Email me:arunkalagarh@gmail.com
No comments:
Post a Comment