Step Template Powershell Script Parameter Mapping

We have OD 2018.8.x. I have a step template with an inline Powershell script, that takes two parameters:

param(
    [parameter(Mandatory = $true)][string[]]$ReceiveHostInstanceNames
    ,[parameter(Mandatory = $false)][string[]]$OrchestrationHostInstanceNames
)

How do I map the step template’s parameters to the script parameters? They have the same name but OD passes nothing to my script at run-time, failing the script before it starts with the following error:

D:\Octopus3\Work\20190304145208-61936-410\Script.ps1 : Cannot process command because of one or more missing mandatory parameters: ReceiveHostInstanceNames.
At D:\Octopus3\Work\20190304145208-61936-410\Bootstrap.Script.ps1:1034 char:1


Thanks,
Peter

Hi Peter,

Thanks for getting in touch! Sorry to see you’re having issues accessing the templates variables in your script.

From what I can tell, you’re trying to pass the variables to your script as powershell parameters - unfortunately, this isn’t supported (though, as I think about it, I’m really beginning to think that’s a good approach we should support).

Instead, you need to either access the variables via $OctopusParameters["ReceiveHostInstanceNames"] or just plain old $ReceiveHostInstanceNames. The variables will automatically be made available to your script.

I also note you’re typing the variables as string[] - these will come in as string instead, so you’ll need to split them on carriage returns.

Let me know how you get along with that.

I’m going to take your approach to the team to see if we could support it - I think it’d make for more testable scripts and easier to understand ones as well.

Hope that helps!

Cheers,
Matt

Thanks, it works!
It would have saved me time if that had been clear in the documentation, though.

Hi Peter

Glad to hear you’ve got it working.

Regarding the documentation, if we changed the page on custom scripting so that instead of saying:

Octopus allows you to define variables to parameterize your deployments. These variables, along with some predefined variables, will be available from within your scripts.

it said:

Octopus allows you to define variables to customize your deployments. These variables, along with some predefined variables, will automatically be made available to your scripts as global variables.

Would that have helped you get to success faster?

Cheers,
Matt

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