Skip to content

Instantly share code, notes, and snippets.

@jhochwald
Last active March 29, 2016 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhochwald/35607bdabea3b0fb7e29 to your computer and use it in GitHub Desktop.
Save jhochwald/35607bdabea3b0fb7e29 to your computer and use it in GitHub Desktop.
Show domains in your Exchange on-premises or Online environment are not being used
#region License
<#
Author: Jos Lieben (https://www.linkedin.com/in/joslieben)
Contributor: Joerg Hochwald (https://www.linkedin.com/in/jhochwald)
Copyright: (c) 2016 by Jos Lieben and Joerg Hochwald. All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
By using the Software, you agree to the License, Terms and Conditions above!
#>
#endregion License
function Get-UnusedDomains {
<#
.SYNOPSIS
Show domains in your Exchange on-premises or Online environment are not being used
.DESCRIPTION
Show domains used and unused domains in your on-premises or Online Exchange organization.
You can select the type of domain to search for.
.PARAMETER DomainType
Type of the Domain to search for? The default is ALL.
Valid values are: Authoritative, InternalRelay, ExternalRelay, All
.PARAMETER Cloud
Search within Exchange Online / Office 365? Default is NO.
.EXAMPLE
PS C:\> Get-UnusedDomains -DomainType 'ALL' -Cloud
Shows a list of used and unused domains in you Exchange Online (Office 365) organization. It search for all domain types.
.EXAMPLE
PS C:\> Get-UnusedDomains -DomainType 'Authoritative'
Shows a list of used and unused domains in you Exchange on-premises organization. Just crawl for domains the organization is "Authoritative" for.
.EXAMPLE
PS C:\> Get-UnusedDomains
Shows a list of used and unused domains in you Exchange on-premises organization. It search for all domain types.
.NOTES
This function is based on an idea of Jos Lieben.
.LINK
Source http://www.lieben.nu/liebensraum/2016/03/finding-unused-accepted-domains-in-exchange-2013/
#>
[CmdletBinding(ConfirmImpact = 'None',
SupportsShouldProcess = $true)]
param
(
[Parameter(ValueFromPipeline = $true,
Position = 1,
HelpMessage = 'Type of the Domain to search for? The default is ALL.')]
[ValidateSet('Authoritative', 'InternalRelay', 'ExternalRelay', 'All')]
[ValidateNotNullOrEmpty()]
[System.String]$DomainType = "All",
[Parameter(Position = 2,
HelpMessage = 'Search within Exchange Online / Office 365? Default is NO.')]
[switch]$Cloud = $false
)
BEGIN {
# Cleanup
$Recipient = $null
$Domains = $null
$Result = @()
if ($Cloud) {
$Recipient = (Get-CloudRecipient -ResultSize unlimited -Filter * | Select-Object EmailAddresses)
} else {
$Recipient = (Get-Recipient -ResultSize unlimited -Filter * | Select-Object EmailAddresses)
}
if ($DomainType -eq "All") {
$Domains = (Get-AcceptedDomain)
} else {
$Domains = (Get-AcceptedDomain | Where{ $_.DomainType -eq "$DomainType" })
}
}
PROCESS {
foreach ($domain in $domains) {
$Object = New-Object PSObject
$Object | Add-Member NoteProperty domainName($domain.DomainName)
$Object | Add-Member NoteProperty domainType($domain.DomainType)
$InUse = $Recipient | where-object { $_.EmailAddresses -Match $domain.DomainName }
if ($InUse) {
$Object | Add-Member NoteProperty inUse("YES")
} else {
$Object | Add-Member NoteProperty inUse("NO")
}
$Result += $Object
}
}
END {
# Dump the Result
Write-Output $Result
# Cleanup
$Recipient = $null
$Domains = $null
$Result = @()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment