Script Repository


Connect to Exchange with PowerShell

January 19, 2018
977

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 Office 365
    $objectId = [Guid]$Context.TargetObject.Get("adm-O365ObjectId")
}
catch
{
    return # The user doesn't have an Office 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 
    
    # 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

    # 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 ( 0 )
No results found.
Leave a comment