Skip to content

Instantly share code, notes, and snippets.

@zplume
Created January 22, 2019 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zplume/a23cae4eaca2ef81e485a936370d7326 to your computer and use it in GitHub Desktop.
Save zplume/a23cae4eaca2ef81e485a936370d7326 to your computer and use it in GitHub Desktop.
param(
# Credentials paramter should be a variable containing the result of Get-Credential
[Parameter(Mandatory = $true)]
[pscredential]$Credentials,
[Parameter(Mandatory = $true)]
[string]$ConfigFile
)
$ErrorActionPreference = "Stop"
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credentials -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking | Out-Null
try {
# Confirm connection to Exchange Online and cmdlets are available
$testCommand = Get-Command "Get-DistributionGroup" -EA "Stop"
Write-Host "`nCreating mail-enabled Azure AD security groups"
Write-Host "`nImporting $ConfigFile...`n"
Import-Csv -Path $ConfigFile | ForEach-Object {
try {
$group = Get-DistributionGroup -Identity $_.GroupName
Write-Host "Group '$($_.GroupName)' exists, skipping..." -ForegroundColor Yellow
}
catch {
Write-Host "Creating Mail-enabled security group '$($_.GroupName)'..."
New-DistributionGroup -Name $_.GroupName -Alias $_.MailNickName -Type "Security"
}
}
}
catch {
Write-Error "Unable to connect to Exchange Online"
}
Remove-PSSession $Session
GroupName MailNickName
Mail-enabled Security Group MailEnabledSecurityGroup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment