Script Repository


Connect to Exchange with PowerShell

August 27, 2021
8236

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 = $Context.CloudServices.CreateExchangeOnlinePSSession()
    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 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 ( 4 )
avatar
Anas El Harda
Feb 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
Feb 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.

avatar
Nick Gatt
Jul 27, 2021
We just need to connect to 365 and run commands across all mailboxes, rather than the one in context. How is this achieved please? We need to connect to 365, and block IMAP/POP on all mailboxes. We then need to run the command weekly to remediate any mailboxes that might have been created insecurely. All ideas appreciated.
avatar
Support
Jul 27, 2021
Hello Nick,

There is no need to use any scripts. It can be done using the built-in Modify Exchange properties action in a scheduled task. The task will include the If has Exchange mailbox condition.

If some of the mailboxes are not in Exchange Online and should not be affected by the task, use the following script from our repository in the task condition:https://www.adaxes.com/script-repository/check-whether-user-has-mailbox-in-exchange-online-s303.htm.
Leave a comment

Related Scripts