Thursday, April 3, 2008

EMS Goodies: Distribution Groups and PowerShell

Here are a few PowerShell scripts that I cobbled together to help with our current mailbox migrations from Exchange 2003 to Exchange 2007. The first, grp_and.ps1, takes the names of two distribution groups as parameters and lists all users who are present in both groups. The second grp_not.ps1, lists all users who are in the first group but NOT in the second group. Note that Exchange 2007 SP1 is required to make use of MemberOfGroup in the recipient filter.


grp_and.ps1

# Script for listing which recipients can be found in both groups passed as arguements

function Usage
{
""
"Usage: .\grp_and [DistributionGroup1] [DistributionGroup2]"
" Lists accounts that are found in both distribution groups."
" Two arguments required."
""
exit
}

# Help Request
if ( ( $Args -eq '-?') -or ($Args.count -ne 2) )
{
Usage
}


$grp1 = $(Get-DistributionGroup $args[0]).Identity.DistinguishedName
$grp2 = $(Get-DistributionGroup $args[1]).Identity.DistinguishedName

""
$grp1
" and"
$grp2
" both contain the following recipients..."
"-------------------------------------------------------"
get-recipient -RecipientPreviewFilter {(MemberofGroup -eq $grp1) -and (MemberofGroup -eq $grp2)}
"-------------------------------------------------------"


grp_not.ps1

# Script for listing which recipients are in the first group but not the second, passed as arguements

function Usage
{
""
"Usage: .\grp_not.ps1 [DistributionGroup1] [DistributionGroup2]"
" Lists accounts that are found in the first group but not the second."
" Two arguments required."
""
exit
}

# Help Request
if ( ( $Args -eq '-?') -or ($Args.count -ne 2) )
{
Usage
}


$grp1 = $(Get-DistributionGroup $args[0]).Identity.DistinguishedName
$grp2 = $(Get-DistributionGroup $args[1]).Identity.DistinguishedName
""
"The following recipients are in "
$grp1
"but are not in "
$grp2
"----------------------------------------------------------------"
get-recipient -RecipientPreviewFilter {(MemberofGroup -eq $grp1) -and (MemberofGroup -ne $grp2)}
"----------------------------------------------------------------"

No comments: