From 1c955a477769fd6af1f6ad3433f178036651669f Mon Sep 17 00:00:00 2001 From: "Jeremy D. Berkleef" Date: Mon, 3 May 2021 00:07:56 -0300 Subject: [PATCH] Add existing scripts --- alias-toolkit.ps1 | 118 +++++++++++++++++++++++++++++++++++++ list-alias-csv-content.ps1 | 14 +++++ search-email-addresses.ps1 | 26 ++++++++ 3 files changed, 158 insertions(+) create mode 100644 alias-toolkit.ps1 create mode 100644 list-alias-csv-content.ps1 create mode 100644 search-email-addresses.ps1 diff --git a/alias-toolkit.ps1 b/alias-toolkit.ps1 new file mode 100644 index 0000000..a04d26d --- /dev/null +++ b/alias-toolkit.ps1 @@ -0,0 +1,118 @@ +function Show-Menu { + param ( + [string]$Title = 'Alias Toolkit by Jeremy Berkleef' + ) + Clear-Host + Write-Host -ForegroundColor Green "================ $Title ================" + + Write-Host "1: Press" -NoNewline + Write-Host -ForegroundColor Cyan " '1' " -NoNewline + Write-Host "to list the contents of a .csv." + + Write-Host "2: Press" -NoNewline + Write-Host -Foregroundcolor Cyan " '2' " -NoNewline + Write-Host "to apply the contents of a .csv to the currently logged in Exchange Online session." + + Write-Host "3: Press" -NoNewline + Write-Host -ForegroundColor Cyan " '3' " -NoNewline + Write-Host "to create a .csv file of aliases from Exchange based on a search term." + + Write-Host "4: Press" -NoNewline + Write-Host -ForegroundColor Cyan " '4' " -NoNewline + Write-Host "to use the legacy search script and output the alias list to console." + + Write-Host "Q: Press" -NoNewline + Write-Host -ForegroundColor Red " 'Q' " -NoNewline + Write-Host "to quit." +} + + +do +{ + Show-Menu + $selection = Read-Host "Please make a selection" + switch ($selection) + { + '1' { + #Ask where the .csv is + $CsvLocation = Read-Host -Prompt 'CSV path' + + #Print a new line + Write-Host `n + + #Read the .csv and print it to console + Import-Csv $CsvLocation | ForEach-Object { + Write-Host -Foregroundcolor Red -Backgroundcolor White (Write-Output $_.Displayname); + write-Host -foregroundcolor Green -Separator `n (Write-Output $_.EmailAddresses.Split(";")) + } + } + '2' { + #Ask where the .csv is and import the values into the Alias_List variable + $CsvLocation = Read-Host -Prompt 'CSV path' + $Alias_list = Import-Csv $CsvLocation + + #Print a new line + Write-Host `n + + #Read the .csv, apply the aliases and print the resulting aliases + Foreach ($Alias in $Alias_list) { + Set-Mailbox -Identity $Alias.Displayname -EmailAddresses $Alias.EmailAddresses.Split(";") + } + + #Get the mailboxes from Exchange Online and print the email addresses to confirm the changes. + Foreach ($Alias in $Alias_list) { + $addresses = (Get-ExoMailbox -Identity $Alias.Displayname).EmailAddresses + if ($addresses -eq $Alias.EmailAddresses) { + Write-Host "$($Alias.Displayname)" -NoNewline + Write-Host -ForegroundColor Green " Success!" + } + else { + Write-Host "$($Alias.Displayname)" -NoNewline + Write-Host -ForegroundColor Red " FAILURE" + } + } + } + '3' { + #Ask the user what we're looking for and we're we're going to save the .csv + $searchTerm = Read-Host -Prompt 'Input search query' + $CsvSavePath = Read-Host -Prompt 'Input location to save .csv' + + #List Aliases and select + get-mailbox | Where-Object {$_.EmailAddresses -like "$searchTerm"} | Select-Object Displayname,@{Name="EmailAddresses";Expression={$_.EmailAddresses}} | Export-Csv -NoTypeInformation $CsvSavePath + + #Remind the user that they need to fix the csv formatting. + Write-Host -ForegroundColor Red -BackgroundColor Black "Please be sure to remember to format the .csv correctly before importing it back into an Exchange Server or Exchange Online" + Write-Host `n + Write-Host -BackgroundColor Black -ForegroundColor Blue "Before importing, delete the non-email addresses from the .csv and replace the spaces between addresses with ';'.`nThis is a safety measure to prevent applying an incompatible address." + } + '4' { + # Ask the user what we need to search for + $searchTerm = Read-Host -Prompt 'Input Query' + + #List Aliases + get-mailbox | Where-Object {$_.EmailAddresses -like "$searchTerm"} |ForEach-Object{ + + $host.UI.Write("White", $host.UI.RawUI.BackGroundColor, "`nUser Name: " + $_.DisplayName+"`n") + + for ($i=0;$i -lt $_.EmailAddresses.Count; $i++) + { + $address = $_.EmailAddresses[$i] + + $host.UI.Write("Green", $host.UI.RawUI.BackGroundColor, $address.AddressString.ToString()+"`t") + + if ($address.IsPrimaryAddress) + { + $host.UI.Write("Blue", $host.UI.RawUI.BackGroundColor, "Primary Email Address`n") + } + else + { + $host.UI.Write("Red", $host.UI.RawUI.BackGroundColor, "Alias`n") + } + } + } + } + } + Read-Host -Prompt 'Press Enter to Continue' +} +until ($selection -eq 'q') + diff --git a/list-alias-csv-content.ps1 b/list-alias-csv-content.ps1 new file mode 100644 index 0000000..a2033b2 --- /dev/null +++ b/list-alias-csv-content.ps1 @@ -0,0 +1,14 @@ +# Author: Jeremy Berkleef +# This script will list the output of the generated alias .csv in console + +#Ask where the .csv is +$CsvLocation = Read-Host -Prompt 'CSV path' + +#Print a new line +Write-Host `n + +#Read the .csv and print it to console +Import-Csv $CsvLocation | ForEach-Object { + Write-Host -Foregroundcolor Red -Backgroundcolor White (Write-Output $_.Displayname); + write-Host -foregroundcolor Green -Separator `n (Write-Output $_.EmailAddresses.Split(";")) + } \ No newline at end of file diff --git a/search-email-addresses.ps1 b/search-email-addresses.ps1 new file mode 100644 index 0000000..384f812 --- /dev/null +++ b/search-email-addresses.ps1 @@ -0,0 +1,26 @@ +# By: Jeremy Berkleef +# The following script will search email addresses based on the search term and spit out all the aliases +# Ask the user what we need to search for +$searchTerm = Read-Host -Prompt 'Input Query' + +#List Aliases +get-mailbox | Where-Object {$_.EmailAddresses -like "$searchTerm"} |ForEach-Object{ + + $host.UI.Write("White", $host.UI.RawUI.BackGroundColor, "`nUser Name: " + $_.DisplayName+"`n") + + for ($i=0;$i -lt $_.EmailAddresses.Count; $i++) + { + $address = $_.EmailAddresses[$i] + + $host.UI.Write("Green", $host.UI.RawUI.BackGroundColor, $address.AddressString.ToString()+"`t") + + if ($address.IsPrimaryAddress) + { + $host.UI.Write("Blue", $host.UI.RawUI.BackGroundColor, "Primary Email Address`n") + } + else + { + $host.UI.Write("Red", $host.UI.RawUI.BackGroundColor, "Alias`n") + } + } +}