Today, I share with you my code to build a function to create a New Distribution Group with PowerShell.
When I create the function, one of my concerns is to facilitate the usage with PSWA. So I don't want to create a CSV with the name of the member before I create my distribution group and call the CSV in my script. I want to do it at one stage. So that's my function does. For me, all my distribution groups are in the same OU so my function is built for that.
Feel free to use and modify my code. If you made some improvements, please share it.
<#
.Synopsis
Connect a remote session to the Exchange 2010 server.
Create a new distribution group with this options :
You can choose if you want the RequireSenderAuthenticationEnabled True (Internal DG) or False (External DG)
You can hide the group from the Exchange Global Address List.
You can ADD at the same time all members needed for the group.
.DESCRIPTION
This cmdlet need to have access to the cmdlet of Exchange.
Its script to be used with Exchange 2010.
Version 1.3
Script by Jeremie Lauzier - Network Administrator, 2015-06-05
.EXAMPLE
Create a new distribution group (Not hidden, no member, need to be authentificated)
New-XXXDistributionGroup -DGname _StarLab -DGalias StarLab -senderneedauthen True -HideDG NotHide
New-XXXDistributionGroup -DGname "_Star Lab" -DGalias StarLab
.EXAMPLE
Create a new distribution group for external use (NO authentification needed)
New-XXXDistributionGroup -DGname _StarLab -DGalias StarLab -senderneedauthen False -HideDG Hide
.EXAMPLE
Create a new distribution group for internal use (Authentification needed)
New-XXXDistributionGroup -DGname _StarLab -DGalias StarLab -senderneedauthen True -HideDG Hide
.EXAMPLE
Create a new distribution group hidden of the Exchange Global Address List
New-XXXDistributionGroup -DGname _StarLab -DGalias StarLab -senderneedauthen True -HideDG Hide
.EXAMPLE
Create a new distribution group with member(s) without CSV needed.
The syntax is really important. You need to use the username of the member.
New-XXXDistributionGroup -DGname _StarLab -DGalias StarLab -senderneedauthen True -HideDG NotHide -member "wellsh","allenb","ramonc"
#>
function New-XXXDistributionGroup
{
[CmdletBinding()]
[OutputType([int])]
Param
(
# Define the name and SamAccountName for the distribution group
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
$DGname,
# Define the email alias (Alias is the first part of the email address)
# For me, in my prod environement, the alias is not the same of the group name.
[Parameter(Mandatory=$true)]
$DGalias,
# Define if RequireSenderAuthenticationEnabled it's True or False
# True = Internal only / False = External Authorized
[Parameter(Mandatory=$true)]
[ValidateSet("True","False")]
$senderneedauthen,
# Hide from Exchange Address List
# It's mandatory just to force to take the right decision
[Parameter(Mandatory=$true)]
[ValidateSet("Hide","NotHide")]
$HideDG,
#Define member(s) of the group
$member
)
Begin
{
$exchsession=New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://yourserveraddress/powershell
Import-PSSession $exchsession -AllowClobber
$convertmem = @($member)
}
Process
{
New-DistributionGroup -Name $DGname -OrganizationalUnit 'yourOU' -SamAccountName $DGname -Alias $DGalias
if ($senderneedauthen -eq "False")
{
Set-DistributionGroup $DGname -RequireSenderAuthenticationEnabled $false
}
if ($HideDG -eq "Hide")
{
Set-DistributionGroup $DGname -HiddenFromAddressListsEnabled $true
}
if ($convertmem -ne $null)
{
$convertmem | foreach {Add-DistributionGroupMember $DGname -Member $_ -BypassSecurityGroupManagerCheck}
}
Get-DistributionGroup $DGname | Format-Table -Wrap
Get-DistributionGroupMember $DGname
}
End
{
Remove-PSSession $exchsession
}
}
No comments:
Post a Comment