Users.ps1

<#
    .SYNOPSIS
        Using a CSV file as a data source, add users to Security Groups and to Distribution lists.
    .DESCRIPTION
        Using a CSV file as a data source, add users to Security Groups and to Distribution lists.
    .LINK
        Nexus Innovations : http://www.nexusinno.com
#>


function global:Add-UsersToGroups {
    [CmdletBinding()]
    Param (
        [ValidateScript({ Test-Path $_ -PathType Leaf })]
        [Parameter(Mandatory)]
        [string]$InputDataPath,

        [Parameter(Mandatory)]
        [string]$StoredKey
    )
    # Connect to Microsoft Online Services (Msol and Exchange)
    Connect-OnlineServices -StoredKey $StoredKey

    # Get Group list - full
    $groups = Get-MsolGroup -MaxResults 50000

    # Load csv file
    $csvObject = Import-Csv $InputDataPath

    # Process csv contents
    $csvObject | ForEach-Object {
        Add-UserToSecurityGroup -GroupName $_.SecurityGroup -UserEmail $_.EMAIL -Groups $groups
        Add-UserToDistributionList -ListName $_.DistributionList -UserEmail $_.EMAIL
    }
}

function script:Connect-OnlineServices {
    Param(
        [string]$StoredKey
    )
    # Retrieve credentials from Windows Credential Store
    $psCredentials = Get-StoredCredential -Target $StoredKey

    # Connect to Microsoft Online Service (for Security Groups)
    Connect-MSolService -Credential $psCredentials

    # Connect to Exchange Online (for Distribution Lists)
    $psSession = Get-PSSession
    if((($null -eq $psSession)) -or (-not ($psSession | Where-Object{$_.ConfigurationName -eq "Microsoft.Exchange"})))
    {
        $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri `
        https://outlook.office365.com/powershell-liveid/ -Credential $psCredentials `
        -Authentication Basic -AllowRedirection `
    
        Import-PSSession $Session -DisableNameChecking
    }
}

function script:Add-UserToDistributionList {
    Param(
        [string]$ListName,
        [string]$UserEmail
    )

    if($null -eq (Get-User -Identity $UserEmail)) {
        Write-Output "ERROR: (DIST LIST ADD) User $($UserEmail) not found."
    } else {
        if($null -eq (Get-DistributionGroup -Identity $ListName)) {
            Write-Output "ERROR: User $($UserEmail) cannot be added to $($ListName); Distribution List not found."
        } else {
            Add-DistributionGroupMember -Identity $ListName -Member $UserEmail
            Write-Output "SUCCESS: User $($UserEmail) added to $($ListName)."
        }
    }
}

function script:Add-UserToSecurityGroup {
    Param(
        [string]$GroupName,
        [string]$UserEmail,
        $Groups
    )

    $userId = Get-MsolUser -UserPrincipalName $UserEmail | Select-Object -ExpandProperty ObjectId
    if($null -eq $userId) {
        Write-Output "ERROR: (GROUP ADD) User $($UserEmail) not found."
    } else {
        $groupId = $Groups | Where-Object { $_.DisplayName -eq $GroupName } | Select-Object -ExpandProperty ObjectId
        if($null -eq $groupId) {
            Write-Output "ERROR: User $($UserEmail) cannot be added to $($GroupName); Group not found."
        } else {
            Add-MsolGroupMember -GroupObjectId $groupId -GroupMemberType User -GroupMemberObjectId $userId
            Write-Output "SUCCESS: User $($UserEmail) added to $($GroupName)."
        }
    }
}