# encoding: ascii
# api: powershell
# title: Convert-BounceToX500
# description: Modification on several other methods, but this one doesn’t load any assemblies and will convert any +xx sequence to their ASCII equivalent.
# version: 0.1
# type: script
# author: Dan Jeuch
# license: CC0
# x-poshcode-id: 5760
# x-archived: 2015-03-15T15:01:47
# x-published: 2015-03-02T00:00:00
#
#
#.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
{
}
PROCESS
{
	if($_) {$bounceAddress = $_}
	$bounceAddress = $bounceAddress -replace "^IMCEAEX-","" -replace "/","\/" -replace "_","/" -replace "@.*$",""
	# The following replaces all "+xx" strings with their ASCII equivalent
	While ($bounceAddress -match "\+([0-9a-f][0-9a-f])")
	{
		$bounceAddress=$bounceAddress -replace ("\$($matches[0])",[char][byte][convert]::ToInt16($matches[1],16))
	}
	$bounceAddress
}
