Powershell Script Template

Just a quick post to cover layout/format of all PowerShell scripts I write.

This default template ensures that a sufficient amount level of of quality is ensured in all scripts. There are several things you will notice:

  • All scripts start with param definitions, which include items for the functionality for that script (both mandatory and option) as well as variables used to support features other basic functions
  • Email alerting on success of failure with option to turn off or on both, either, or no email alerts
  • I use exit calls, I know this is probably considered bad, but as you can see, it makes it easier to troubleshoot

 

And here it is (you can download it here):

 

# ==============================================================================================
#
# Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 2009
#
# NAME: <name of script which will be helpful>
#
# AUTHOR: <author>
# DATE  : <date>
#
# COMMENT: <Step through what this script does
#
# ==============================================================================================

#<place comments used to assist in the calling of the script

param(
    #<application specific params>
    
    #<email and alerting params>
    [Parameter(Position=3, Mandatory=$false, HelpMessage="Specify email alert recipient address")]$recipient="[email protected]",
    [Parameter(Position=4, Mandatory=$false, HelpMessage="Specify email alert from address")]$from="[email protected]",
    [Parameter(Position=5, Mandatory=$false, HelpMessage="Specify smtp server name/ip")]$smtpserver="smtp.server.com",
    [Parameter(Position=6, Mandatory=$false, HelpMessage="Alert on sucessfuly copy")]$emailonsuccess=$true,
    [Parameter(Position=7, Mandatory=$false, HelpMessage="alert on failure of copy")]$emailonfailure=$true,
    [Parameter(Position=8, Mandatory=$false, HelpMessage="alert email subject")]$emailsubject="<change to be helpful subject>"
)

#
#variable declarations
        

function send-email ($body, $success)
{
    #incase we are troublshooting, output the body here
    $body
    
    $error.clear()
    #if it is sucessful
    if ($success)
    {
        #if we want a successful emails
        if ($emailonsuccess)
        {
            $subject = $emailsubject + "- Success"
            Send-MailMessage -To $recipient -From $from -SmtpServer $smtpserver -Subject $subject -Body $body -bodyashtml
        }                 
    }
    else #if unsuccessful
    {
        #if we want emails on failure
        if ($emailonfailure)
        {       
            $subject = $emailsubject + "- Failure"
            Send-MailMessage -To $recipient -From $from -SmtpServer $smtpserver -Subject $subject -Body $body -bodyashtml
        }                    
    }
    
    if ($error)
    {
        "Unable to send an email"
        exit 666
    }                                                                                                                  
}                                                                                                                           

#
# If we expect a crash/error here is an example (this is using $error but it could be try catch or based on $lastexitcode for external calls
#
$error.clear()
#do something bad
somethingbad()
if ($error)
{
    send-email "<sad>" $false
    exit 7
}


#email that we got here successfully
send-email "<HAPPY>" $true