Featured image of post Automating with PowerShell: Deploying Send as Alias for M365

Automating with PowerShell: Deploying Send as Alias for M365

So Microsoft has finally caught up and now allows users to send emails from their aliases, a feature we’ve all been waiting for. To be able to send as an alias you’ll need to do two things. The first is to run the script that enabled the “Send From Alias” option. The second is to add the alias manually to the From Field. You do this by going to Outlook/OWA and selecting the from field. Click on “Other e-mail address” and manually enter the alias.

The script you’ll need to run is below and enables this option for all of your tenants, you can run this on a schedule using an Azure Function or just on demand.

The Script

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
######### Secrets #########
$ApplicationId = 'ApplicationID'
$ApplicationSecret = 'ApplicationSecret' | ConvertTo-SecureString -Force -AsPlainText
$TenantID = 'YourTenantID'
$RefreshToken = 'Refreshtoken'
$ExchangeRefreshToken = 'ExchangeToken'
$UPN = "A-Valid-UPN"
######### Secrets #########
$credential = New-Object System.Management.Automation.PSCredential($ApplicationId, $ApplicationSecret)

$aadGraphToken = New-PartnerAccessToken -ApplicationId $ApplicationId -Credential $credential -RefreshToken $refreshToken -Scopes 'https://graph.windows.net/.default' -ServicePrincipal -Tenant $tenantID
$graphToken = New-PartnerAccessToken -ApplicationId $ApplicationId -Credential $credential -RefreshToken $refreshToken -Scopes 'https://graph.microsoft.com/.default' -ServicePrincipal -Tenant $tenantID

Connect-MsolService -AdGraphAccessToken $aadGraphToken.AccessToken -MsGraphAccessToken $graphToken.AccessToken
$customers = Get-MsolPartnerContract -All
foreach ($customer in $customers) {
    write-host "Logging into tenant $($customer.DefaultDomainName)" -ForegroundColor Green
$token = New-PartnerAccessToken -ApplicationId 'a0c73c16-a7e3-4564-9a95-2bdf47383716'-RefreshToken $ExchangeRefreshToken -Scopes 'https://outlook.office365.com/.default' -Tenant $customer.TenantId
    $tokenValue = ConvertTo-SecureString "Bearer $($token.AccessToken)" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($upn, $tokenValue)
    $customerId = $customer.DefaultDomainName
    $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell-liveid?DelegatedOrg=$($customerId)&BasicAuthToOAuthConversion=true" -Credential $credential -Authentication Basic -AllowRedirection
    Import-PSSession $session -AllowClobber -CommandName 'Set-OrganizationConfig', 'get-OrganizationConfig'
    #From here you can enter your own commands
    $Enabled = (get-OrganizationConfig).SendFromAliasEnabled
    if ($Enabled) {
write-host "Already enabled for $($Customer.DefaultDomainName)" -ForegroundColor Yellow
}
else {
write-host "Enabling for $($Customer.DefaultDomainName)" -ForegroundColor Green
Set-OrganizationConfig -SendFromAliasEnabled $True
}
#end of commands
Remove-PSSession $session

}

And that’s it! as always, Happy PowerShelling.

All blogs are posted under AGPL3.0 unless stated otherwise
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy