Reputation: 4566
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
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
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