Variables That Contain Spaces Or Special Characters Don't work

kpatella's Avatar

kpatella

13 Oct, 2017 12:43 PM

Create a variable "Test Variable With Spaces" in Library->Variable Sets. Set the value to anything

write the following script

Get-Variable |%{ "Name : {0}`r`nValue: {1}`r`n" -f $_.Name,$_.Value }

$test1 = Get-Variable "Test Variable With Spaces" -Valueonly;

Write-Output ("Test1: " + $test1);

If you look at the variables when it runs on the machine I am seeing it become "TestVariableWithSpaces" where all spaces were removed.

I have the same issue with a variable named "Test-Variable.With,Special%chars"

This is a sample of my output:

Name : TestVariableWithSpaces
October 13th 2017 08:25:15Info
Value: F:\Test
October 13th 2017 08:25:15Info
Name : TestVariableWithSpecialchars
October 13th 2017 08:25:15Info
Value: G:\Test
October 13th 2017 08:25:15Info
Name : true
October 13th 2017 08:25:15Info
Value: True
October 13th 2017 08:25:15Info
Name : VerbosePreference
October 13th 2017 08:25:15Info
Value: SilentlyContinue
October 13th 2017 08:25:15Info
Name : WarningPreference
October 13th 2017 08:25:15Info
Value: Continue
October 13th 2017 08:25:15Info
October 13th 2017 08:25:15Error
Get-Variable : Cannot find a variable with the name 'Test Variable With 
October 13th 2017 08:25:15Error
Spaces'.
October 13th 2017 08:25:15Error
At C:\Octopus\Work\20171013122513-887\Script.ps1:4 char:10
October 13th 2017 08:25:15Error
+ $test1 = Get-Variable "Test Variable With Spaces" -Valueonly;
October 13th 2017 08:25:15Error
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
October 13th 2017 08:25:15Error
    + CategoryInfo          : ObjectNotFound: (Test Variable With Spaces:Strin 
October 13th 2017 08:25:15Error
   g) [Get-Variable], ItemNotFoundException
October 13th 2017 08:25:15Error
    + FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.G 
October 13th 2017 08:25:15Error
   etVariableCommand
October 13th 2017 08:25:15Error
October 13th 2017 08:25:15Error
The remote script failed with exit code 1
  1. Support Staff 1 Posted by Tom Peters on 16 Oct, 2017 02:58 AM

    Tom Peters's Avatar

    Hi,

    As described here, you can access Octopus variables in powershell scripts in two ways. The first way is $MyVariable, where MyVariable is the name of an Octopus Deploy variable with special characters and spaces stripped out. This is what you are seeing.

    The other way of accessing variables in powershell is by using the provided OctopusParameters dictionary. So to access the same variable, you would use $OctopusParameters["MyVariable"]. This second form preserves any spaces or special characters, so in your example you could access this same variable with $OctopusParameters["Test Variable With Spaces"].

    Hope that helps. Let me know if you have any more questions :)
    Tom

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac