PowerShell Octopus Variable Not Working

Hi,
We have Octopus 2.0 and we are trying to automate one of our BI source project. One of the step is to run the source workflow file, here is the working command in PowerShell:

$t = '"D:\Test\UsageTracking.iqp" /wf "Default Workflow" /e "Dev"'

Invoke-Command -ComputerName $DBServer -ScriptBlock {
param ([string] $t1 = $t)

    $status = Start-Process "C:\Program Files\iQ4bis\HaloSource\HaloSource.Run.exe" $t1 -Wait -PassThru
    $test2 = $status.ExitCode
        if ($test2 -ne 0) 
        { 
            Throw "The command exited with error code: $test2"
        }
        else
        {
            Write-host "Workflow executed successfully."    
        }
} -ArgumentList $t

My issue is, when I am trying to put this whole value:

‘“D:\Test\UsageTracking.iqp” /wf “Default Workflow” /e “Dev”’

in a Octopus Variable like the one attached in the screenshot attachment,it fails with following error message:

20:38:35Info
‘“D:\Test\UsageTracking.iqp” /wf “Default Workflow” /e “Dev”’
20:38:38Error
The command exited with error code: 3
20:38:38Error
At D:\Octopus\Work\20160401003832-1041\Script.ps1:4 char:1
20:38:38Error

  • Invoke-Command -ComputerName $DBServer -ScriptBlock {
    20:38:38Error

20:38:38Error
+ CategoryInfo : OperationStopped: (The command exited with error code: 3:String) [], RuntimeException
20:38:38Error
+ FullyQualifiedErrorId : The command exited with error code: 3
20:38:38Error
20:38:38Fatal
The remote script failed with exit code 1

***Note: I have also attached our PowerShell step as an attachment

This is our PowerShell step

Hi Hiren,

Thanks for getting in touch. From what I can see, the problem is because you want the variable $Workflow to be evaluated like script, instead of a value, and PowerShell will be auto-quoting and encoding where you didn’t expect it to.

Octopus is optimized to provide variables to your script as “values”, not as “script” - it encodes the variable values so when they are used by PowerShell as, say, a Password, or argument to another command, it will work as expected.

You can think about this like encoding a block of JavaScript to display on a Web Page: you don’t want that JavaScript to be executed. Octopus treats variables in a similar way - you can use them in your scripts as values, but you can’t always use them as script.

My suggestion would be to take the $Workflow and break it into several variables, and then have your script build the command.

Here’s an example…

Variables:

WorkflowFile: UsageTracking.iqp
WorkflowName: Default Workflow

Script:

$status = Start-Process "HaloSource.Run.exe" $WorkflowFile /wf $WorkflowName /e $OctopusEnvironmentName -Wait -PassThru

The nice thing is PowerShell will take care of quoting the $WorkflowFile and $WorkflowName and $OctopusEnvironmentName values properly on your behalf.

Hope that helps!
Mike

Sorry Michael I was on a long break and just came back yesterday. Thank your reply and I will add that to our list and see how it goes.

Thank you,
Hiren Patel