Is is possible to include Octopus variables in a PowerShell script step? The UI makes it seem like this is possible, but the actual script output just includes the variable names and not the intended output of the variables being replaced with their actual values.
I am using 2.5.11.
Hi,
Thanks for getting in touch! So hopefully I am on to something here. The example variable that you have in your script will work but only for listening tentacles.
I was able to get mine to show:
Script:
Write-Host $OctopusParameters["Octopus.Machine.Name"]
Write-Host $OctopusParameters["Octopus.Machine.Hostname"]
Output:
WIN2014R2 6
localhost
PowerShell exit code: 0
Is that the issue here? Did you try other variables?
Vanessa
Thank you for the response.
My initial confusion was thinking that the PowerShell step was doing token-style replacement. Now I realize that is not the case.
However, I am seeing that while variables such as $OctopusParameters["Octopus.Machine.Name"]
are working properly, other custom variables like $ServiceName
are not working, they just contain empty values during this step.
I know that the $ServiceName
does work as I use the same variable in previous steps in the deployment process.
I’ve changed my script slightly to this
$ServerName = "\\\\" + $OctopusParameters['Octopus.Machine.Hostname']
Write-Host "Setting $ServiceName credentials on $ServerName"
& sc.exe $ServerName query $ServiceName
& sc.exe $ServerName stop $ServiceName
& sc.exe $ServerName config $ServiceName obj= $ServiceAccount
& sc.exe $ServerName config $ServiceName password= $ServicePassword
Not that I am using both $OctopusParameters and custom variables which are defined in my variables for the project. Only the $OctopusParameters variables have values.
The output from the Write-Host statement in the script above during a release is
Setting credentials on \\my-server
Notice that the $ServiceName variable inserted an empty value between Setting
and credentials
My issue turned out to be that this new PowerShell step I added was not included in the variable’s scope.
Sorry about the noise, hopefully this will help someone else.