Script Repository


Connect to Exchange with PowerShell

April 28, 2020
3857

The script demonstrates how to connect to Exchange from Adaxes with the help of PowerShell.

Exchange Online

Edit Remove
PowerShell
try
{
    # Get the object ID in Microsoft 365
    $objectId = [Guid]$Context.TargetObject.Get("adm-O365ObjectId")
}
catch
{
    return # The user doesn't have a Microsoft 365 account
}

try
{
    # Connect to Exchange Online
    $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" `
        -Credential $Context.GetOffice365Credential() -Authentication Basic -AllowRedirection
    Import-PSSession $session -AllowClobber -DisableNameChecking -CommandName "Set-Mailbox" 
    
    # Change mailbox type
    Set-Mailbox $objectId.ToString() -Type Shared
    # TODO: replace with the code you need to execute
}
finally
{
    # Close the remote session and release resources
    if ($session) { Remove-PSSession $session }
}

Exchange Online using EXO V2 module

To use the script, install the EXO V2 PowerShell module on the computer where Adaxes service is running.

Edit Remove
PowerShell
Import-Module ExchangeOnlineManagement

try
{
    # Get the object ID in Microsoft 365
    $objectId = [Guid]$Context.TargetObject.Get("adm-O365ObjectId")
}
catch
{
    return # The user doesn't have a Microsoft 365 account
}

Connect-ExchangeOnline -Credential $Context.GetOffice365Credential()

# Change mailbox type
Set-Mailbox $objectId.ToString() -Type Shared
# TODO: replace with the code you need to execute

Exchange On-Premises

Parameter:

  • $exchangeServer - specifies the Fully Qualified Domain Name (FQDN) of your Exchange Server.
Edit Remove
PowerShell
$exchangeServer = "ExchangeServer.domain.com" # TODO: Modify me

try
{
    # Connect to Exchange Server
    $session = New-PSSession -connectionURI "http://$exchangeServer/powershell" -ConfigurationName Microsoft.Exchange
    Import-PSSession -session $session -CommandName "Set-Mailbox"

    # Change mailbox type
    Set-Mailbox "%distinguishedName%" -Type Shared
    # TODO: replace with the code you need to execute
}
finally
{
    # Close the remote session and release resources
    if ($session) { Remove-PSSession -Session $session}
}


Comments ( 2 )
avatar
Anas El Harda
February 18, 2020

Tried both method 1 & 2 with no sucess, the command i'm trying to run is "Add-DistributionGroupMember -Identity TestGroup -Member testuser1 -Confirm:$False"

I see no errors thrown and when i check the TestGroup in O365 the user is not added.

avatar
Support
February 18, 2020

Hello Anas,

You can use the below script to add the target user to the group whose name is specified in the $groupName variable.

Edit Remove
PowerShell
Import-Module ExchangeOnlineManagement

$groupName = "MyGroup" # TODO: modify me

try
{
    # Get the object ID in Office 365
    $objectId = [Guid]$Context.TargetObject.Get("adm-O365ObjectId")
}
catch
{
    return # The user doesn't have an Office 365 account
}

Connect-ExchangeOnline -Credential $Context.GetOffice365Credential()

# Add member to group
try
{
    Add-DistributionGroupMember $groupName -Member $objectId.ToString() -ErrorAction Stop
}
catch
{
    $Context.LogMessage("An error occurred when adding the user to $groupName group. Error: " + $_.Exception.Message, "Warning")
}

If you need to automate membership in Office 365 groups, have a look at the following scripts from our repository: https://www.adaxes.com/script-repository/add-user-to-office-365-distribution-groups-based-on-business-unit-membership-s254.htm.

Leave a comment

Related Scripts