Empty variables

This thread adequately poses my question, however I find the answer somewhat unsatisfying, so I hope you can help me clarify a few things.

Let’s assume we have #{address} that was not given, let also assume that we have code like this: $a = "#{address}" - note the quotation marks. As the author of the original question claims, $a variable will not be a empty string or a null in will contain characters #{address} verbatim, starting with hash, the opening left curly bracket, etc. All in all the substitution is not happening.

I understand the example without the quotation marks, but it is problematic in different aspects.

Let’s run the script that the support staff suggested:

$a = #{address}
if ($a -eq $null) {
 Write-Host "address was null"
}

But let’s set the variable to read 1 Microsoft Way
If the variable is set then the script suggested by the support staff simply errors out.

At E:\Octopus\Work\20170904053104-27405-161\Script.ps1:1 char:8
+ $a = 1 Microsoft Way
+        ~~~~~~~~~
Unexpected token 'Microsoft' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken
The remote script failed with exit code 1
testvar on the Octopus Server

Let’s agree that the script that only works on a very specific variable value is not very useful script.

Thus the question, why does the variable does not get substituted if it is empty? It’s rather awkward to program against. Any suggestions?

Hi Andrew,

There is actually another way to retrieve variable values in PowerShell, you can use $OctopusParameters["address"] and this way you can then check if value is null.
See https://octopus.com/docs/deploying-applications/custom-scripts#Customscripts-VariablesinPowerShellscripts

Cheers
John

Thank you.