Skip to content

Instantly share code, notes, and snippets.

@zplume
Created January 22, 2019 16:12
Show Gist options
  • Select an option

  • Save zplume/a23cae4eaca2ef81e485a936370d7326 to your computer and use it in GitHub Desktop.

Select an option

Save zplume/a23cae4eaca2ef81e485a936370d7326 to your computer and use it in GitHub Desktop.

Revisions

  1. zplume created this gist Jan 22, 2019.
    39 changes: 39 additions & 0 deletions Create-MailEnabledSecurityGroups.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    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
    2 changes: 2 additions & 0 deletions MailEnabledGroups.csv
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    "GroupName","MailNickName"
    "Mail-enabled Security Group","MailEnabledSecurityGroup"