ProxyAddressUpdate.psm1
<#
=========================================================================== Created by: Rhys M Contact: RhysM.PS@gmail.com PS Gallery: https://www.powershellgallery.com/profiles/RhysM/ Filename: ProxyAddressUpdate.psm1 ------------------------------------------------------------------------- Module Name: ProxyAddressUpdate =========================================================================== #> function ProxyAddressUpdate { Param ( [Parameter(Position = 0, Mandatory = $False)] $SearchBase, [Parameter(Position = 0, Mandatory = $False)] $Individual, [Parameter(Position = 1, Mandatory = $True)] [string]$NewProxyAddress, [Parameter(Position = 2, Mandatory = $False)] [ValidateSet('Primary', 'Secondary')] $Type = 'Secondary' ) $PrimarySMTP = "SMTP:" $SecondarySMTP = "smtp:" if ($SearchBase) { foreach ($SelectedOU in $Searchbase) { $GetOU_Users = Get-ADUser -Filter * -SearchBase $SelectedOU -Properties ProxyAddresses Foreach ($OUAccount in $GetOU_Users) { if ($OUAccount.ProxyAddresses.count -eq 0) { $UpdatedProxyAddress = $OUAccount.SamAccountName + "@$NewProxyAddress" if ($Type -eq "Primary"){ Set-ADUser -Identity $OUAccount.SamAccountName -Add @{Proxyaddresses="$PrimarySMTP"+$UpdatedProxyAddress} } elseif($Type -eq "Secondary"){ Set-ADUser -Identity $OUAccount.SamAccountName -Add @{Proxyaddresses="$SecondarySMTP"+$UpdatedProxyAddress} } } else { $UpdatedProxyAddress = $OUAccount.SamAccountName + "@$NewProxyAddress" foreach ($CurrentProxyAddress in $OUAccount.ProxyAddresses){ if ($CurrentProxyAddress -cmatch $PrimarySMTP -or $Type -eq "Primary"){ $MailAddress = $OUAccount.Mail $null, $OldPrimarySMTP = $CurrentProxyAddress -split $PrimarySMTP $ConvertToSecondary = $SecondarySMTP + $OldPrimarySMTP Set-ADUser -Identity $OUAccount.SamAccountName -Remove @{Proxyaddresses="$CurrentProxyAddress"} Set-ADUser -Identity $OUAccount.SamAccountName -Add @{Proxyaddresses="$PrimarySMTP"+$UpdatedProxyAddress} Set-ADUser -Identity $OUAccount.SamAccountName -Add @{Proxyaddresses="$ConvertToSecondary"} Set-ADUser -Identity $OUAccount.SamAccountName -Replace @{Mail="$PrimarySMTP" + "$UpdatedProxyAddress"} } elseif($Type -eq "Secondary"){ Set-ADUser -Identity $OUAccount.SamAccountName -Add @{Proxyaddresses="$SecondarySMTP"+$UpdatedProxyAddress} } } } } } } elseif ($Individual) { $GetOU_User = Get-ADUser -Filter "SamAccountName -like '$Individual'" -Properties ProxyAddresses $UpdatedProxyAddress = $GetOU_User.SamAccountName + "@$NewProxyAddress" if ($GetOU_User.ProxyAddresses.count -eq 0) { if ($Type -eq "Primary"){ Set-ADUser -Identity $GetOU_User.SamAccountName -Add @{Proxyaddresses="$PrimarySMTP"+$UpdatedProxyAddress} } elseif($Type -eq "Secondary"){ Set-ADUser -Identity $GetOU_User.SamAccountName -Add @{Proxyaddresses="$SecondarySMTP"+$UpdatedProxyAddress} } } else { foreach ($CurrentProxyAddress in $GetOU_User.ProxyAddresses){ if ($CurrentProxyAddress -cmatch $PrimarySMTP -or $Type -eq "Primary"){ $MailAddress = $OUAccount.Mail $null, $OldPrimarySMTP = $CurrentProxyAddress -split $PrimarySMTP $ConvertToSecondary = $SecondarySMTP + $OldPrimarySMTP Set-ADUser -Identity $GetOU_User.SamAccountName -Remove @{Proxyaddresses="$CurrentProxyAddress"} Set-ADUser -Identity $GetOU_User.SamAccountName -Add @{Proxyaddresses="$PrimarySMTP"+$UpdatedProxyAddress} Set-ADUser -Identity $GetOU_User.SamAccountName -Add @{Proxyaddresses="$ConvertToSecondary"} Set-ADUser -Identity $GetOU_User.SamAccountName -Replace @{Mail="$PrimarySMTP" + "$UpdatedProxyAddress"} } elseif($Type -eq "Secondary"){ Set-ADUser -Identity $GetOU_User.SamAccountName -Add @{Proxyaddresses="$SecondarySMTP"+$UpdatedProxyAddress} } } } } } function ProxyAddressUpdate-RemoveCurrent { Param ( [Parameter(Position = 0, Mandatory = $False)] $SearchBase, [Parameter(Position = 0, Mandatory = $False)] $Individual ) $PrimarySMTP = "SMTP:" $SecondarySMTP = "smtp:" if ($SearchBase) { foreach ($SelectedOU in $Searchbase) { $GetOU_Users = Get-ADUser -Filter * -SearchBase $SelectedOU -Properties ProxyAddresses Foreach ($OUAccount in $GetOU_Users) { if ($OUAccount.ProxyAddresses.count -eq 0) { $Output = $OUAccount.SamAccountName + " has no proxy addresses" Write-Verbose $Output } else { foreach ($CurrentProxyAddress in $OUAccount.ProxyAddresses){ Set-ADUser -Identity $OUAccount.SamAccountName -Remove @{Proxyaddresses="$CurrentProxyAddress"} $Output = $OUAccount.SamAccountName + " Proxy Address: $CurrentProxyAddress Removed" Write-Verbose $Output } } } } } elseif ($Individual) { $GetOU_User = Get-ADUser -Filter "SamAccountName -like '$Individual'" -Properties ProxyAddresses if ($GetOU_User.ProxyAddresses.count -eq 0) { $Output = $GetOU_User.SamAccountName + " has no proxy addresses" Write-Verbose $Output } else { foreach ($CurrentProxyAddress in $GetOU_User.ProxyAddresses){ Set-ADUser -Identity $GetOU_User.SamAccountName -Remove @{Proxyaddresses="$CurrentProxyAddress"} $Output = $GetOU_User.SamAccountName + " Proxy: $CurrentProxyAddress Removed" Write-Verbose $Output } } } } Export-ModuleMember -Function ProxyAddressUpdate, ProxyAddressUpdate-RemoveCurrent |