What is the correct way to use PowerShell switch in step templates?

I have a PowerShell script that is packaged into a NuGet package. The script contains parameters similar to the following:

  [Parameter(Mandatory=$True)][string] $databaseName,
  [Parameter][switch] $blockOnPossibleDataLoss

I am trying to figure out the best way to call this script from a step template, where the step template has a variable that determines if the switch should be set. I haven’t been able to get it to work using either switch parameters or boolean parameters.

I understand that Octopus passes all variables as strings. What is the “approved” or best way to use variables for switches and/or bool parameters?

Hi Erick,

Thanks for getting in touch.

To pass a switch that checks for the existence of -blockOnPossibleDataLoss you can use a conditional expression that checks the value of the variable.

For example: given a variable called blockOnPossibleDataLoss and a script with the parameters databaseName and blockOnPossibleDataLoss configure the following parameters in Octopus:

-databaseName MyDatabaseName #{if blockOnPossibleDataLoss}-blockOnPossibleDataLoss#{/if}

I hope this helps.


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.