Back to Summer Batch home page

Template PowerShell script

<#
Never use Write-Host as it writes out to Host UI screen.

PowerShellTasklet expects that $global:ScriptExitStatus is set in the script on exit, 
see examples below
$global:ScriptExitStatus = [Summer.Batch.Core.ExitStatus]::Completed
$global:ScriptExitStatus = [Summer.Batch.Core.ExitStatus]::Failed

or user defined ExitStatus...[Summer.Batch.Core.ExitStatus] must be used 
to strong type $userExitStatus 

[Summer.Batch.Core.ExitStatus]$userExitStatus = 
	New-Object Summer.Batch.Core.ExitStatus("UserExitCode","Description")
$global:ScriptExitStatus = $userExitStatus
#>

Function Format-Error ($thisError) 
{
    [string] $errMsg = $thisError.Exception.Message
    $errMsg += "`r`n`r`n"
    $errMsg += $thisError.InvocationInfo.PositionMessage
    Return $errMsg
}

# Enable messaging...
$VerbosePreference = "Continue"
$DebugPreference = "Continue"

# if you need to trace at statement level...
#Set-PSDebug -Trace 2

# Let user know script file being executed...
$scriptFile = $MyInvocation.MyCommand.Definition
Write-Output "Start Executing Script File: $scriptFile"

# $global:ScriptExitStatus is pre-set from PowerShellTasklet in a GLOBAL scope...
Write-Output "Initial ScriptExitStatus => $($global:ScriptExitStatus.ExitCode)"

try
{
	#Make all errors terminating
	$ErrorActionPreference = "Stop"; 

	#=> start user code here...
    #=> end user code here...

	# make sure to set ScriptExitStatus in a global scope...
	$global:ScriptExitStatus = [Summer.Batch.Core.ExitStatus]::Completed

	Write-Output "On Exit ScriptExitStatus => $($global:ScriptExitStatus.ExitCode)"

} catch {
	
	# let PowerShellTasklet know something failed...
	[string] $errString = Format-Error($_)
	Write-Error $errString -ErrorAction Continue

	# using built-in ExitStatus...if set will be used by PowerShellTasklet...
	$global:ScriptExitStatus = [Summer.Batch.Core.ExitStatus]::Failed

	#=> Exit Script
    Exit 1

} finally {
 
   $ErrorActionPreference = "Continue"; #Reset the error action pref to default

}

#Trace Message...
Write-Output "End Executing Script File: $scriptFile"

# must specify exit or return status...
Exit 0
			
Back to Summer Batch home page