Block Coding / Code Iterations

When building a complex script in PowerShell, remember to bite off only what you can chew successfully. This means that you probably can’t code an entire script one setting, so don’t. A complex script will take time, it needs to be coded in chunks and in many iterations. Why? Experience will tell you that mistakes will be made, compromises will occur, error correcting will be skipped. Let’s walk through a sample scenario.

Example

Your IT Manager comes to you with a request to create a script that the help desk or someone else in the IT department can use to clean up certain emails in one or more mailboxes. The script needs to have a menu and separate items to search and report, delete emails and possible soft delete emails. This one lends to the process of compartmentalization. We can break down the script into parts for the menu and for each function that the script needs to perform.

Menu
[sourcecode language=”powershell”]
$Menu = {
Write-Host " **********************" -ForegroundColor Cyan
Write-Host " Mailbox Cleanup Script" -ForegroundColor Cyan
Write-Host " **********************" -ForegroundColor Cyan
Write-Host " "
Write-Host " 1) Find Emails"
Write-Host " 2) Report on emails found – notify IT Manager"
Write-Host " 3) Soft delete emails"
Write-Host " 4) Hard delete emails"
Write-host " "
Write-Host " 99) Exit" -ForegroundColor Red
Write-Host " "
Write-Host " Select an option.. [1-99]? " -ForegroundColor White -NoNewLine
}
[/sourcecode]
Visual menu:

That’s one block of self-contained code.

Calling Menu Options

Below shows a sample of a called function:
[sourcecode language=”powershell”]
Function FindEmails {
<CODE BLOCK>
} # End Function 'FindEmails'
[/sourcecode]
After coding it, this is when the concept of iterations come into place. First, get a basic working model. Can it find emails in a test mailbox? If so, then move on to adding error
handling in case a mailbox cannot be contact or no emails can be found.

Sample Error Handling:
[sourcecode language=”powershell”]
Try {
$Removal = Get-Mailbox $User | Search-Mailbox -SearchQuery $MessageQuery –DeleteContent -ErrorAction STOP
} Catch {
Write-host 'The attempt to remove the email message failed.' -ForegroundColor Yellow
}
[/sourcecode]
Once that is good, add comments to the function. Finally run through more testing with test mailboxes and live data if at all possible to better gauge the code’s success.
Repeat this same process for each function needed in the script. Lastly, add a comment block at the top to give meaning to the script for yourself, your manager and the IT Department. We can do it like so:
[sourcecode language=”powershell”]
#############################################
# SCRIPT DETAILS
# Good description of the script
#
# SCRIPT VERSION HISTORY
# Current Version : 1.0
# Change Log : 1.0 – First iteration
#
# OTHER SCRIPT INFORMATION
# Wish list : New Cool features
# Rights Required : Local admin on server
# Exchange Version : 2019
# Author : Damian Scoles
# My Blog : http://justaucguy.wordpress.com
# Disclaimer : You are on your own.
#
#. \ScriptName.PS1
#############################################
[/sourcecode]
A finished script should then be assembled to see if each function works as expected. Once testing confirms the script is successful, a demo or live data test should be performed with your IT manager and then put into production after it is approved.

Following a process like this will make building scripts more production and hopefully less frustrating. Keep in mind that reusing code samples can speed up even a process like this. the more you code, the better you will get.

Pro Tip
Can’t get around a problem? Seek out someone with more PowerShell knowledge then you have.

Related Post

A Good EndingA Good Ending

Like all good scripts, starting off well should be reciprocated with a good ending as well. What does that mean? Think processing, cleanup, ending transcripts, truncating log files and more.