PoshCode Archive  Artifact [47c9834fd3]

Artifact 47c9834fd318a6f935a5705907cf953b1200dd986bc4137e44f77988358278b1:

  • File XC_SMTPFunctions.ps1 — part of check-in [80f40ccd4d] at 2018-06-10 13:27:54 on branch trunk — Report on all email addresses per mailbox. Optionally remove regex matched addresses. Add any custom address thru parsing any prefix in the loop of SMTP domains. (user: chriskenis size: 2363)

# encoding: ascii
# api: powershell
# title: XC_SMTPFunctions
# description: Report on all email addresses per mailbox. Optionally remove regex matched addresses. Add any custom address thru parsing any prefix in the loop of SMTP domains.
# version: 0.1
# type: function
# author: chriskenis
# license: CC0
# x-poshcode-id: 3580
# x-archived: 2012-08-17T15:00:25
# x-published: 2012-08-14T04:21:00
#
#
param(
[Parameter(Position=0,ValueFromPipeline=$True)]
[ValidateNotNullorEmpty()][string[]]$Mailboxes = @(),
[string] $Regex = "domain\.local$|^CCMAIL|^MS:COMPREGION",
$SMTPdomains = @("domain.com","sub.domain.com"),
$Delimiter = "|",
$OutputLog = "C:\Projects\XC\smtpadresses.csv",
[switch] $Output,
[switch]$Remove,
[switch]$Update
)

Function NewSMTPAddress{
param(
$Mailbox,
$Prefix,
$Domain
)
$Address = "$Prefix@$Domain"
$Mailbox | Set-Mailbox -EmailAddresses @{add = $Address}
OutputMB -Mailbox $Mailbox -Address $Address -Match $False -Action "Added"
}

Function RemoveSMTPAddress{
param(
$Mailbox,
$Address,
$Match
)
$Mailbox | Set-Mailbox -EmailAddresses @{remove = $Address}
OutputMB -Mailbox $Mailbox -Address $Address -Match $Match -Action "Removed"
}

Function OutputMB{
param(
$Mailbox,
$Address,
$Match,
$Action
)
$global:objOut += New-Object -Typename PSObject -Property @{
	Mailbox = $Mailbox
	Address = $Address
	Match = $Match
	Action = $Action
	}
}

$global:objOut = @()

ForEach ($Mailbox in $Mailboxes){
	try{
		$sMailbox = get-mailbox -identity $Mailbox -EA Stop
		foreach ($Address in $sMailbox.EmailAddresses){
			if([regex]::IsMatch($Address,$Regex)){
				if ($Remove){RemoveSMTPAddress -Mailbox $sMailbox -Address $Address -Match $True}
				else{OutputMB -Mailbox $sMailbox -Address $Address -Match $True -Action "Matched"}
				}
			else{OutputMB -Mailbox $sMailbox -Address $Address -Match $False -Action "None"}
		}
		if ($Update){
			foreach ($sDomain in $SMTPdomains){
				NewSMTPAddress -Mailbox $sMailbox -Prefix $sMailbox.SamAccountName -Domain $sDomain
				}
			}
		}
	catch{write-warning "no such mailbox: $Mailbox";continue}
}

if($Output){
	$objOut | Select Mailbox, Address, Match, Action | ConvertTo-Csv -Delimiter $Delimiter -NoTypeInformation | out-file $OutputLog -append
	}
else {$objOut | Select Mailbox, Address, Match, Action}