fix-aliasv20.ps1

# ================================================
#
# This script is based on the original fix-alias.ps1 script created by Matthew Byrd (matbyrd@microsoft.com)
# This script allows you to identify and modify users, contact, distributions lists and Public Folder
# that have invalid characters in the alias.
#
# With this version it’s possible to search for multiple incorrect characters and replace them. Besides this chage it is possible
# to fix incorrect Public Folder aliases.
#
# Last Modified 21-7-2011
# Author: johan@johanveldhuis.nl
#
# ================================================

# Input Parameters
param ([string]$type = “”,$resultsize = “Unlimited”,[string]$search = ” “,[string]$search2 = ” “,[string]$replace = “”,[string]$replace2 = “”,$add = $null,[switch]$help=$false)

# Variables

[array]$baseobjects # Array to hold all found objects
[string]$new # Value of the new alias
$choice # Used to determine choice in switch commands
$command # For holding the constructed command to execute
[string]$filter = ‘ -Filter {(alias -notlike “Schedule”) -and (alias -notlike “Offline Address”) -and (alias -notlike “Schema”) -and (alias -notlike “OAB”) -and (alias -notlike “Organization Forms”) -and (alias -notlike “microsoft”) -and (alias -notlike “default”)}’ #filter to exclude some system public folders
# Formating Variables
# Allows a Central place for defining the colors of script messages

[string]$info = “White” # Color for informational messages
[string]$warning = “Yellow” # Color for warning messages
[string]$error = “Red” # Color for error messages

# ShowHelp Function (help about_function)
# ================================================

Function ShowHelp {

Write-host “This script will find objects of the specified type that contain a space in the alias”
Write-host “It will remove the space from the alias and update the object”
Write-host ” ”
Write-host “Both the search character and the replacement character can be changed using the advanced options”
Write-host “Advanced Options:” -foregroundcolor $warning
write-host ” ”
Write-host “-Type : Used to specifiy the get- command that is run to find the objects (Mailbox,Distributiongroup,Mailcontact,MailPublicFolder)”
write-host “-Resultsize : Used to specifiy a result size other than the default of `”Unlimited`””
write-host “-Search : Used to specify the character / sting to search for”
write-host “-Replace : Used to specify the replacement character”
write-host “-Add : Used to provide the get- command with addtional switch options”
write-host “-Help : Display this help message”
write-host ” ”
Write-host “Examples:” -foregroundcolor $info
write-host ” ”
Write-host “fix-alias.ps1 -type MailContact -Search `”@`” -Replace `”_`” -add `”-OrganizationalUnit ‘My Ou’`””

}
# Function to gather and modify the alias (help about_function)
# ================================================

Function FixObject {

# Use iex (invoke-expression) to execute the cmdlet in $command (help invoke-expresion)
# Place the output in the $baseobjects array

$baseobjects = iex $getcommand

# Loop thru all of the object in the $baseobjects array (help about_foreach)
# If the alias of the object contains the search character/characters operate on the object (help about_if)
# Otherwise Do Nothing

foreach ($value in $baseobjects) {
if (($value.alias -like “*$search*”) -and ($value.alias -like “*$search2*”))
{
# Write out that we found an object to modify
# Use the string.replace .net method to search for and replace the character
# http://msdn2.microsoft.com/en-us/library/fk49wtc1(vs.90).aspx
# Write out the New Alias
# Construct the Set command into a variable and execute using iex

write-host “Found Object to Fix:” $value.alias -foregroundcolor $error

$new = $value.alias
$new = $new.replace($search,$replace)
$new = $new.replace($search2,$replace2)

write-host “New Alias of Object:” $new -foregroundcolor $info
write-host ” ”

$setcommand = “set-” + $type + ” ‘” + $value.identity + “‘ -alias $new”
iex $setcommand

}
else { }
}

}

# Main Body of Script
# ================================================

# Display help if -help specified (help about_if)

if ($help -eq $true)
{ ShowHelp;exit }
else { }

# Determine if $type is set by parameter (help about_if)
# Provide the user a list of choices if $type is not set

if ( $type.length -eq 0 )
{

# Provide user with choice of objects to check

Write-host ” ”
write-host “Please Choose what objects to search for”
write-host ” ”
write-host “1 – Mailbox”
write-host “2 – Contacts”
Write-host “3 – Distribution Group”
Write-host “4 – Public Folder”

# Capture the users choice (help read-host)
$choice = read-host “Choice (1,2,3,4)”

# Using the switch command evaluate the input and set the $type variable (help about_switch)
# If an out of bounds choice is made show the script help

switch ($choice) {
1 {$type = “Mailbox”}
2 {$type = “MailContact”}
3 {$type = “distributiongroup”}
4 {$type = “mailpublicfolder”}
default {write-host ” “;write-host “Incorrect options Specified: $choice” -foregroundcolor $error;ShowHelp;exit}
}

}
else { }

# Constuct the command to be executed into the $getcommand variable

$getcommand = “get-” + $type + ” -resultsize $resultsize ” + $add + $filter

# Call the FixObject function to fix the objects

FixObject