Sendmarc Microsoft 365 Set PostMaster PowerShell Script

PowerShell Script to set the PostMaster address in MS365

!Important!

Sendmarc provides no support for PowerShell scripts and these are executed at your own risk. 

Sendmarc takes no responsibility for any issues that this script might cause.

Please read through the code before executing the script

 Please copy the contents of this script below into a TXT file and rename the file as "Sendmarc Sendmarc Microsoft 365 PostMaster Enabler.ps1"

Should the script successfully execute, a log file will be created at C:\Sendmarc\nameofscript.txt

Please review this article on how to execute a PowerShell Script:

How To Run PowerShell Scripts

Script Objective

The objective of this script is to allow you to set the PostMaster address in MS365

The script will automatically prompt you for input upon execution to set a PostMaster address.

The script will query you to login into MS365, please ensure the account you are logging in with has access to security.microsoft.com

Script Contents

#SENDMARC POSTMASTER ENABLER
#Author: Brian Goosen - brian@sendmarc.com
#Usage of this Script is at your own risk.
#COPYRIGHT SENDMARC
#V2.0

#Changelog
#Added Functions to drive application flow.

$hostGui = $Host.UI.RawUI
$HostGui.WindowTitle = "Sendmarc MS365 PostMaster Enabler V1.0"
$HostGui.ForegroundColor = "White"

# Check if the script is running with administrative privileges
$isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

# If not running as admin, relaunch the script with elevated permissions
if (-not $isAdmin) {
    Write-Host "This script requires administrative privileges. Please run the script as an administrator." -ForegroundColor Cyan
    
    # Relaunch the script with elevated permissions
    Start-Process powershell.exe -Verb RunAs -ArgumentList "-File $($MyInvocation.MyCommand.Path)"
}

# Generate a timestamp for the log file name
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
# Create a log file path
$logFilePath = "C:\Sendmarc\Sendmarc_PostMaster_Enable_LogFile_$timestamp.txt"
# Start logging
Start-Transcript -Path $logFilePath

Write-Host "==================================================" -ForegroundColor Green
Write-Host "Sendmarc MS365 PostMaster Enabler" -ForegroundColor Green
Write-Host "Author: Brian Goosen" -ForegroundColor Green
Write-Host "Email: Brian@sendmarc.com" -ForegroundColor Green
Write-Host "COPYRIGHT SENDMARC" -ForegroundColor Green
Write-Host "==================================================" -ForegroundColor Green

function LaunchApplication {
    #Confirm Application Launch
    $title = 'Configure MS365 PostMaster'
    $question = 'Do you want to continue?'
    $choices = '&Yes', '&No'

    $decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
    if ($decision -eq 0) {
        ConnecttoMS365
        GetCurrentPostmaster
        SetPostMaster
    }
    else {
        Write-Host "Script Aborted" -ForegroundColor Red
        Exit
    }
}

function ConnecttoMS365 {
    Write-Host "Checking if ExchangeOnlineManagement is installed" -ForegroundColor Cyan
    if (-not (Get-Module -ListAvailable -Name ExchangeOnlineManagement)) {
        Write-Host "Installing ExchangeOnlineManagement Powershell Module" -ForegroundColor Cyan
        Install-Module -Name ExchangeOnlineManagement -Force -Scope CurrentUser
    }
    else {
        Write-Host "ExchangeOnlineManagement is installed" -ForegroundColor Cyan
    }

    Write-Host "Importing ExchangeOnlineManagment" -ForegroundColor Cyan
    Import-Module ExchangeOnlineManagement
    Write-Host "Connecting to MS365" -ForegroundColor Cyan
    Write-Host "Please Log into MS365" -ForegroundColor Cyan
    Connect-ExchangeOnline
    Write-Host "Connected to MS365" -ForegroundColor Cyan
}

function GetCurrentPostmaster {
    Write-Host "Getting Current PostMaster Address" -ForegroundColor Cyan
    Write-Host "Curernt PostMaster Address is:" -ForegroundColor Cyan
    Get-TransportConfig | Format-List ExternalPostmasterAddress
}

function SetPostMaster {
    #Confirm Postmaster Change
    $title = 'Change Postmaster Address?'
    $question = 'Do you want to continue?'
    $choices = '&Yes', '&No'
    $decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
    if ($decision -eq 0) {
        $postMasterAddress = Read-Host "Enter New Postmaster Address (postmaster@domain.com)"
        Set-TransportConfig -ExternalPostmasterAddress $postMasterAddress
        Write-Host "Getting Current PostMaster Address" -ForegroundColor Cyan
        Write-Host "New PostMaster Address is:"
        Get-TransportConfig | Format-List ExternalPostmasterAddress
    }
    else {
        Write-Host "Script Aborted" -ForegroundColor Red
        return
    }
    
}

#Launch Application
LaunchApplication


Write-Host ""
Write-Host "==================================================" -ForegroundColor Cyan
Write-Host "Thank you for choosing Sendmarc!" -ForegroundColor Cyan
Write-Host "==================================================" -ForegroundColor Cyan

# Stop logging
Stop-Transcript
# Display the path of the log file
Write-Host "Log file created: $logFilePath"
Write-Host "Press Enter to Close This Program" -ForegroundColor Cyan
Read-Host "..."

Need Help?

Sendmarc does not provide any support for PowerShell scripts and are run at your own risk.