PoshCode Archive  Artifact [6b8dc7fdae]

Artifact 6b8dc7fdaecec9d3559c20646a178439394be0adf1009232696bb8365c0d1649:

  • File Convert-BounceToX500.ps1 — part of check-in [7eef78c7ff] at 2018-06-10 13:09:07 on branch trunk — Convert a URL Encoded address most commonly found in bounce messages into an X500 address that can be added as an alias to the appropriate mail-enabled object. (user: Jon Webster size: 1751)

# encoding: ascii
# api: powershell
# title: Convert-BounceToX500
# description: Convert a URL Encoded address most commonly found in bounce messages into an X500 address that can be added as an alias to the appropriate mail-enabled object.
# version: 0.1
# type: script
# author: Jon Webster
# license: CC0
# x-poshcode-id: 2364
# x-derived-from-id: 2547
# x-archived: 2017-04-28T02:53:23
# x-published: 2011-11-15T18:19:00
#
# Added pipeline support
#
# $Id: Convert-BounceToX500.ps1 610 2010-11-16 00:39:19Z jon $
# $Revision: 610 $

#.Synopsis
#  Convert Bounce to X500
#.Description
#  Convert URL Encoded address in a Bounce message to an X500 address
#  that can be added as an alias to the mail-enabled object
#.Parameter bounceAddress
#  URL Encoded bounce message address#
#.Example
#  Convert-BounceToX500 "IMCEAEX-_O=CONTOSO_OU=First+20Administrative+20Group_cn=Recipients_cn=john+5Fjacob+2Esmith@contoso.com"
#.Example
#  "IMCEAEX-_O=CONTOSO_OU=First+20Administrative+20Group_cn=Recipients_cn=john+5Fjacob+2Esmith@contoso.com"|Convert-BounceToX500

[CmdletBinding()]
PARAM (
	[Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$bounceAddress
)
BEGIN
{
	Add-Type -AssemblyName System.Web|Out-Null
}
PROCESS
{
	if($_) {$bounceAddress = $_}
	$bounceAddress = $bounceAddress -Replace "%2B","%" # This is a urlEncoded "+"
	$bounceAddress = $bounceAddress -Replace "%3D","="
	$bounceAddress = $bounceAddress -Replace "\+","%"
	$bounceAddress = $bounceAddress -Replace "_O=","/O="
	$bounceAddress = $bounceAddress -Replace "_OU=","/OU="
	$bounceAddress = $bounceAddress -Replace "_CN=","/CN="

	if([Web.HttpUtility]::UrlDecode($bounceAddress) -match "(/o=.*)@[\w\d.]+$"){$matches[1]}
}