Added Files

This commit is contained in:
DistractADD
2021-07-06 13:16:46 +10:00
parent f2475a3bdd
commit cfddd4fad2
393 changed files with 65842 additions and 0 deletions

View File

@@ -0,0 +1,111 @@
# Script to bulk remove email proxy addresses from Exchange users in a synced environment using an on-prem Exchange management server
#
# What is the FQDN of the on-prem Exchange server?
$exchangeServerFQDN = ''
# Which domains are we removing?
$domainsToRemove = @('','')
# Create Exchange connection Uri from FQDN
$exchangeConnectionUri = 'http://' + $exchangeServerFQDN +'/PowerShell/'
# Establish a session to Exchange
$userCredential = Get-Credential
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $exchangeConnectionUri -Authentication Kerberos -Credential $userCredential
Import-PSSession $session -DisableNameChecking
### Check if address policies are using this domain ###
# Initialise a hastable to store our results in
$addressPolicies = @()
# Get all address policies from Exchange
$allAddressPolicies = Get-EmailAddressPolicy
# Run through the domains checking if the domain to be removed is in the address policies
foreach ($domainToRemove in $domainsToRemove) {
$emailAddressTemplate = 'SMTP:@' + $domainToRemove
$addressPolicies += $allAddressPolicies | Where-Object {$_.EnabledEmailAddressTemplates -contains $emailAddressTemplate}
}
# If domain found in address policies then list them and ask if we want to contiue removing the proxy domain
if ($addressPolicies.Count -gt 0) {
Write-Output -InputObject ('The following address policies are using the domain to be removed:')
$addressPolicies | Format-Table
$continue = ''
while ($continue -notmatch '[YyNn]') {
$continue = Read-Host -Prompt 'Do you want to continue running the script? (Y/N)'
}
if ($continue -match '[Nn]') {
Write-Output -InputObject ('Terminating script.')
break
}
else {
Write-Output -InputObject ('Continuing with script.')
}
}
###
### Remove the domain from mailboxes ###
# Get All Mailboxes
$allMailboxes = Get-RemoteMailbox -ResultSize Unlimited | Sort-Object -Property alias
# Remove alias from each mailbox
foreach ($mailbox in $allMailboxes) {
$redundantAddresses = @()
foreach ($domainToRemove in $domainsToRemove) {
$redundantAddresses += (($mailbox.EmailAddresses -split ',' | Where-Object {$_ -match $domainToRemove}) -replace 'smtp:','')
}
if ($redundantAddresses.Count -gt 0) {
Write-Output -InputObject ('Removing addresses ' + $redundantAddresses + ' from maibox ' + $mailbox.Name)
Set-RemoteMailbox -Identity $mailbox.Identity -EmailAddresses @{remove=$redundantAddresses} -Confirm:$false
}
}
###
### Remove the domain from contacts ###
# Get all the contacts
$allContacts = Get-MailContact -ResultSize Unlimited | Sort-Object -Property alias
# Remove alias from each contact
foreach ($contact in $allContacts) {
$redundantAddresses = @()
foreach ($domainToRemove in $domainsToRemove) {
$redundantAddresses += (($contact.EmailAddresses -split ',' | Where-Object {$_ -like ('*' + $domainToRemove + '*')}) -replace 'smtp:','')
}
if ($redundantAddresses.Count -gt 0) {
Write-Output -InputObject ('Removing addresses ' + $redundantAddresses + ' from contact ' + $contact.Name)
Set-MailContact -Identity $contact.Identity -EmailAddresses @{remove=$redundantAddresses} -Confirm:$false -ForceUpgrade:$true
}
}
###
### Remove the domain from groups ###
# Get all the groups (this includes email enabled security groups)
$allGroups = Get-DistributionGroup -ResultSize Unlimited | Sort-Object -Property alias
# Remove alias from each group
foreach ($group in $allGroups) {
$redundantAddresses = @()
foreach ($domainToRemove in $domainsToRemove) {
$redundantAddresses += (($group.EmailAddresses -split ',' | Where-Object {$_ -like ('*' + $domainToRemove + '*')}) -replace 'smtp:','')
}
if ($redundantAddresses.Count -gt 0) {
Write-Output -InputObject ('Removing addresses ' + $redundantAddresses + ' from group ' + $group.Name)
Set-DistributionGroup -Identity $group.Identity -EmailAddresses @{remove=$redundantAddresses} -Confirm:$false
}
}
###
### Public folders don't feature here because in this scenario they're cloud only ###
# End the Exchange Session
Remove-PSSession -Session $session

View File

@@ -0,0 +1,62 @@
# Find mailboxes for synced accounts, where the mailbox exists in Exchange Online but doesn't exist on-prem as a remote mailbox.
#
# Like the other stuff in this folder, it's a bit of a niche scenario.
# We're looking for mailboxes which were created in on-prem AD and then licenced in the cloud without a remote mailbox being created on the on-prem Exchange server, so that we can remote mail enable them correctly.
#
# This is a bit complicated as it requires connected to two Exchange environments at once, so uses command prefixes for one of them.
#
# What is the FQDN of the on-prem Exchange server?
$exchangeServerFQDN = ''
# What is your remote routing address? E.g.: @domain.mail.onmicrosoft.com
$remoteRoutingSuffix = '@domain.mail.onmicrosoft.com'
# Find and load the new ExO "module"
$exoModulePath = (Get-ChildItem -Path $env:userprofile -Filter CreateExoPSSession.ps1 -Recurse -Force -ErrorAction SilentlyContinue).DirectoryName[-1]
. "$exoModulePath\CreateExoPSSession.ps1"
# Establish a session to Exchange Online
Connect-EXOPSSession
# Create Exchange on-prem connection Uri from FQDN
$exchangeConnectionUri = 'http://' + $exchangeServerFQDN +'/PowerShell/'
# Establish a session to Exchange on-prem and add the OnPrem prefix to all commands
$userCredential = Get-Credential
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $exchangeConnectionUri -Authentication Kerberos -Credential $userCredential
Import-PSSession $session -DisableNameChecking -Prefix OnPrem
# Get list of mailboxes from Exchange online for synced users
$allOnlineMailboxes = Get-Mailbox -ResultSize Unlimited | Where-Object {$_.IsDirSynced -eq $true -and $_.Name -notlike 'DiscoverySearchMailbox*'}
# Get list of remote mailboxes from on-prem server
$allOnPremMailboxes = Get-OnPremRemoteMailbox -ResultSize Unlimited
# Compare the two lists and add those missing from the on-prem list to the $syncedMailboxMismatch hashtable
$syncedMailboxMismatch = $allOnlineMailboxes | Where-Object {$allOnPremMailboxes.PrimarySmtpAddress -notcontains $_.PrimarySmtpAddress}
# Find mailboxes where alias and username match
$matchedUPNAliases = $syncedMailboxMismatch | Where-Object {$_.Alias -eq $_.UserPrincipalName.Split('@')[0]}
# Find mailboxes where alias and username don't match
$mismatchedUPNAliases = $syncedMailboxMismatch | Where-Object {$_.Alias -ne $_.UserPrincipalName.Split('@')[0]}
# Fix the mailboxes where username and alias matched.
foreach ($mailboxToRemoteEnable in $matchedUPNAliases) {
$mailboxUsername = $mailboxToRemoteEnable.UserPrincipalName.Split('@')[0]
$mailboxToFix = $mailboxToRemoteEnable.UserPrincipalName
$remoteRoutingAddress = $mailboxUsername + $remoteRoutingSuffix
Enable-OnPremRemoteMailbox -Identity $mailboxToFix -RemoteRoutingAddress $remoteRoutingAddress
}
# Write out the mailboxes that have been updated
Write-Output -InputObject ('The following mailboxes have been remote mail enabled.')
$matchedUPNAliases | Select-Object Name,Alias,UserPrincipalName,PrimarySmtpAddress
# Write out the mailboxes that have been left due to a mismatch between username and alias
Write-Output -InputObject ('The following mailboxes have not been changed because they have a mismatch between email alias and username.')
$mismatchedUPNAliases | Select-Object Name,Alias,UserPrincipalName,PrimarySmtpAddress
# End the Exchange Session
Remove-PSSession -Session $session

View File

@@ -0,0 +1,24 @@
# Disable a list of mail users
#
# What is the FQDN of the on-prem Exchange server?
$exchangeServerFQDN = ''
# What accounts are we disabling?
$usersToDisable = @('','')
# Create Exchange connection Uri from FQDN
$exchangeConnectionUri = 'http://' + $exchangeServerFQDN +'/PowerShell/'
# Establish a session to Exchange
$userCredential = Get-Credential
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $exchangeConnectionUri -Authentication Kerberos -Credential $userCredential
Import-PSSession $session -DisableNameChecking -AllowClobber
# Get list of mailboxes
foreach ($userToDisable in $usersToDisable) {
Disable-RemoteMailbox -Identity $userToDisable -Confirm:$false
}
# End the Exchange Session
Remove-PSSession -Session $session

View File

@@ -0,0 +1,28 @@
# Find mailboxes using a specific email domain and export list to CSV file
#
# What is the FQDN of the on-prem Exchange server?
$exchangeServerFQDN = ''
# Primary SMTP domain to search for
$primarySMTP = ''
# Where are we saving the output file?
$outputFile = 'C:\Temp\Mailboxes.csv'
# Create Exchange connection Uri from FQDN
$exchangeConnectionUri = 'http://' + $exchangeServerFQDN +'/PowerShell/'
# Establish a session to Exchange
$userCredential = Get-Credential
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $exchangeConnectionUri -Authentication Kerberos -Credential $userCredential
Import-PSSession $session -DisableNameChecking
# Get list of mailboxes
$allMailboxes = Get-RemoteMailbox -ResultSize Unlimited | Where-Object {($_.PrimarySmtpAddress.Split('@')[1] -eq $primarySMTP)}
# Export results to CSV file
$allMailboxes | Select-Object Name,Alias,UserPrincipalName,PrimarySmtpAddress,EmailAddresses | Export-Csv -Path $outputFile -NoTypeInformation
# End the Exchange Session
Remove-PSSession -Session $session

View File

@@ -0,0 +1,15 @@
# Exchange Online with AzureAD Connect
## What is this?
The scripts in this folder are for managing mailboxes in a synced environment, where you have an Exchange server on-prem purely for management purposes. This means all mailboxes are seen as "remote" mailboxes, so all the scripts use commands to that effect. These can easily be updated to a normal environment like on-prem only or cloud only just by removing the work "remote" from the commands. For example, Get-RemoteMailbox to just Get-Mailbox.
## Pre-requisites
These scripts require you to have on-prem Active Directory with an on-prem Exchange server for management, and using AzureAD Connect to sync to AzureAD.
It's pretty niche.
## Disclaimer
All scripts are provided as is without warranty of any kind, use them at your own risk.