Monday, May 5, 2008

Powershell template

I'm working on a template for all powershell scripts in our environment. Basically something everyone can take as a starting point for their scripts, that will guarantee a level of homogeneity of functions (logging, debug level messages, email output, etc.).

Below is what I have so far. Any thoughts on improvements/additions?


##########################################################################
## Script Name : template.ps1 ##
## Created : 05/02/2008 ##
## Author : John McDevitt ##
## Function : sample script to be used as a start for all scripts ##
## : in production. ##
## : ##
## Usage : how to call this script (e.g. arguments required or ##
## : accepted) ##
## : ##
## Host/path : where is this script located ##
## : ##
## Notes : Update the help message with meaningful text for ##
## : your script. ##
## : ##
## : include debugging messages by calling debug_msg ##
## : e.g., debug_msg("about to do something weird") ##
## : include log messages by calling log_msg. Log file ##
## : is configurable, but defaults to a scriptname.log ##
## : in the current directory (will be a share soon). ##
## : All debug messages go into the log prefaced with ##
## : DEBUG: ##
## : ##
## : you will probably need to update the param block, ##
## : even though it is above the "your code here" block ##
## : ##
## Update Log : ##
## : ##
##########################################################################
param (
[switch]$debug,
[string]$mailto,
[string]$logfile
)

function Usage
{
""
"Describe the purpose of this script"
""
"Usage: template.ps1 -option <value> "
""
"Required Parameters:"
" -option <value>: Describe the options and their expected values here"
""
"Optional Parameters:"
" -mailto user@domain: User/group to send a copy of any debugging messages"
" or log info to."
""
" -debug: Enables debug messages -- useful for tracing code execution"
""
" -? : Display this usage information"
""
""
exit
}

function log_msg ($message) {
$message >> $logfile
if ($mailto) { $script:email_body = $script:email_body + $message + "`n" }
}

function debug_msg ($message) {
if ($debug) {
$message = "DEBUG: " + $message
$message
log_msg $message
}
}

log_msg ("started execution at " + (get-date))

if ($logfile -eq "") {
$logfile = $($MyInvocation.mycommand.name) + ".log"
}

debug_msg "logfile is $logfile"

##########################################################################
## YOUR CODE BEGINS BELOW THIS LINE ##
##########################################################################

if (( $ARGS -eq '-?') -or ( $ARGS -match "help" )) {
Usage
}

##########################################################################
## YOUR CODE ENDS ABOVE THIS LINE ##
##########################################################################

log_msg ("completed execution at " + (get-date))
if ($mailto) {
send-smtpmail -to $mailto -smtphost mailhost.yourdomain.com -from $mailto -subject $($MyInvocation.mycommand.name) -body $email_body
}

1 comment:

Anonymous said...

Oh yeah, I feel qualfied to tell you what to add and what to subtract! definitely! :rolleyes:

counter free hit invisible