Step template questions

Trying to write my first powershell step template (Octopus 2.4.9.166). Some of my params should be mandatory and some should be optional but with sane default values. Have some questions.

  1. How to specify param default value as null (not an empty string)?
  2. How to represent a bool default value (not a string value)?

I get this when trying to pass step template variable value of $False to the function in my step template function.

Failed to create new virtual app.
Info    22:33:37
System.Management.Automation.ParameterBindingArgumentTransformationException: Cannot process argument transformation on parameter 'legacyAppPool'. Cannot convert value "System.String" to type "System.Boolean". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0. ---> System.Management.Automation.ArgumentTransformationMetadataException: Cannot convert value "System.String" to type "System.Boolean". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0. ---> System.Management.Automation.PSInvalidCastException: Cannot convert value "System.String" to type "System.Boolean". 

Here is function def and function invocation:

try
{    
	Write-Host "Step template variable values:"
	Write-Host "siteName = $siteName"
	Write-Host "appPoolName = $appPoolName"
	Write-Host "virtualAppName = $virtualAppName"
	Write-Host "enabledProtocols = $enabledProtocols"
	Write-Host "idleTimeout = $idleTimeout"    
	Write-Host "maxProcesses = $maxProcesses"
	Write-Host "legacyAppPool = $legacyAppPool"    

	new-virtualApp -siteName $siteName -appPoolName $appPoolName -legacyAppPool $legacyAppPool -appPoolDotNetVersion $appPoolDotNetVersion -appPoolUser $appPoolUser -appPoolPwd $appPoolPwd -virtualAppName $virtualAppName -enabledProtocols $enabledProtocols -idleTimeout $idleTimeout -maxProcesses $maxProcesses
}
catch [System.Exception]
{
  write-host "Failed to create new virtual app."
  write-host $error[0].Exception.ToString()
  throw
}

function new-virtualApp
{
	param(
	[parameter(mandatory=$False, HelpMessage='Enter the site name, i.e. Default Web Site')]
	[string]$siteName="Default Web Site",
	
	[parameter(mandatory=$True)]    
	[string]$appPoolName,

	[parameter(mandatory=$False)]    
	[bool]$legacyAppPool = $False,
	
	[parameter(mandatory=$False, HelpMessage='Enter the dot net version for the app pool, i.e. v4.0')]
	[string]$appPoolDotNetVersion = "v4.0",        

	[parameter(mandatory=$True)]    
	[string]$appPoolUser,

	[parameter(mandatory=$True)]    
	[string]$appPoolPwd,  

	[parameter(mandatory=$True)]    
	[string]$virtualAppName,

	[parameter(mandatory=$False, HelpMessage='Enter the enabled protocols, i.e. http,net.tcp')]    
	[string]$enabledProtocols = "http",

	[parameter(mandatory=$False, HelpMessage='Enter the number of idle minutes after which the application pool should unload. 0 = no timeout.')]    
	[ValidateRange(0,60)] 
	[int]$idleTimeout = 0,

	[parameter(mandatory=$False, HelpMessage='Enter the maximum number of worker processes.')]    
	[ValidateRange(1,16)] 
	[int]$maxProcesses = 1
	)

Can you do any validation in step template form on user input to help guide the user?

Can you have a step template import an custom / existing powershell module previously defined in Octopus rather than defining a new one? If so, how do you bind the declared template params to the powershell function params?

Where can we see when the step template was last updated? Looking for some way to version these guys.

How do we rename an existing step template?

How do we delete an existing step template? i need to delete some test templates / do-overs.

Hi,

To get started, I suggest you take a look at my article Making Great Octopus Powershell Step Templates. It explains how I use it to ensure parameters have the correct types and to use a default value when none are supplied. To ensure types are correct in PowerShell you use the casting syntax. It looks like your function has strongly typed parameters which should result it the values being casted to the correct types. So you may wish to check the correct values are being passed in.

The step template form does not support any validation at creation time. Currently, the best course of action is to ensure detailed help text for all script parameters and detailed error messages at runtime.

In Octopus, you can create reusable Script modules from the Library > Script modules page. These are slightly different to step templates in that they don’t have any parameters and are included for all script once enabled in a project. You can include a script module by navigating to Project > Process clicking Include script modules and selecting the modules you want.

If you click the export button for a step template, you can see the last modified time and user. There is also a template version number you can see in either the exported json or on the Usage tab for the template. The Octopus Deploy Step Template Library uses a GitHub repository to track the canonical version of all our scripts. However, this isn’t perfect, and we’re currently investigating our options in this space.

To rename or delete a template navigate to Library > Step templates > Your Template > Settings. Here you can change the template name or delete the template. Note that you won’t be able to delete a template while it’s still in use.

Hope that helps!

Daniel