Paradox
Paradox

Reputation: 4566

Octopus variable within string with no space following it?

I am writing a powershell script and would like to do a Octopus variable substitution for the variable $MyVar. However, it needs to be in a string with no spaces such as:

$query = @"SELECT * FROM [$MyVarDB].[SomeSchema].[SomeTable]"@

The issue arises when octopus thinks the variable's name is $MyVarDB when the variable is really $MyVar, followed by the string "DB". Is there a way to escape or enclose $MyVar so that it can be directly followed by a string?

Upvotes: 0

Views: 730

Answers (2)

Jeff Hutton
Jeff Hutton

Reputation: 166

Octopus recognizes the #{my var} format in inline scripts. If MyVar='Hello' for:

$query = @"SELECT * FROM [$#{MyVar}DB].[SomeSchema].[SomeTable]"@

Octopus will parse the script prior to running it and substitute value for the #{MyVar} token so the value becomes $HelloDB.

Note: This only works for scripts using the inline option. If your Octopus script pulls a script from another source (i.e. the deployed package, GitHub) at runtime, it won't have a chance to parse the script prior to running it and substitution won't occur.

Upvotes: 1

benPearce
benPearce

Reputation: 38333

Try writing the Octopus variable value to a private variable first

$MyVarDBValue=$OctopusParameters["My Database Variable"]
$query = @"SELECT * FROM [$MyVarDBValue].[SomeSchema].[SomeTable]"@

Upvotes: 1

Related Questions