I have a script deployment step who’s aim is to run a netsh command. I’m referencing two variables inside the command but was getting a parameter is incorrect error on deployment. I added a Write-Host to output each variable to the deploy log to see what it actually resolves to.
$ServiceUsername resolves correctly to my domain\user string as set in project variables.
$OctopusParameters[‘Octopus.Machine.Name’] resolves incorrectly to System.Collections.Generic.Dictionary’2[System.String,System.String][‘Octopus.Machine.Name’]
Thanks for reaching out. Since that variable is part of a dictionary, to include it into a double quoted text you need to wrap it between
$(). An example:
write-host "The name of my machine is $($OctopusParameters['Octopus.Machine.Name'])
Hope that helps,
Thanks for the response.
The answer is a little bit convoluted but thanks all the same. I should add though that I didn’t write the variable out myself but inserted it from the pop up action searchy thing. That should know in advance that it’s a dictionary and format it appropriately. Otherwise it kind of negates the point of the said pop up action searchy thing.
$() work for you?
$() is just the way Powershell works for dictionary values inside double quotes or cases where you want to print the value of an object property like this:
$process= Get-Process -name Explorer
Write-output "The ID of the Explorer process is: $($process.id)"
If you want to avoid having to put
$() you could assign the value of the Octopus variable to a Powershell variable and then print the latter
$MyMachine = $OctopusParameters['Octopus.Machine.Name']
Write-Output "My machine name is $MyMachine"
Or you could join the strings on the same line as the write-host
Write-Output "My machine name is: " + $OctopusParameters['Octopus.Machine.Name']
Forcing the editor to know in advance that the variable is being used inside of double quotes and adding
$() accordingly sounds more like a feature of a Powershell IDE, and that’s not what we are aiming for with our editor.
I am having similar issue/s
I am running the following script from the task console on octopus server 3.2.19 which works fine:
$var = $OctopusParameters[‘Octopus.Machine.Name’]
write-Host “machine: $var”
then on a different server (188.8.131.521) it doesn’t work, and it returns null
Looking deeper into it, running the following script :
returns only 3 key-value pairs:
and in most of them the InstanceName equals to “Tentacle” in the others it’s the actual machine/tentacle name.
in the script output the first lines are always Sending script to ""
or "Running script on ""
how can I retrieve this () value if the octopus system variable/s are empty for some reason?
The easiest way to know which variables are avaliable during the deployment, is to enable to enable the debugging variables and then checking the raw log. To do so please follow the below steps:
Add these 2 variables to your project http://docs.octopusdeploy.com/display/OD/Debug+problems+with+Octopus+variables
Create a new release (so the new variables take effect) and deploy it. If possible skip as many steps as you can and only leave step we are troubleshooting in order to avoid the noise in the log.
Attach the raw log to the thread http://docs.octopusdeploy.com/display/OD/Get+the+raw+output+from+a+task
Thanks for your reply.
I have 2 problems with the suggestions below:
- I am not getting this as part of a project which I try to deploy, it happens as part of a script that I am running as a task from the script console.
- Attaching all the variables of our projects to a public thread might not be a good idea for security reasons.
Sent from my iPhone
I understand you 2nd point.
My memory might be failing me, but I believe those variables were available in 2.6 as well as in 3.X. Could you please run a script from the Script console with the below content, and then send us the log to support(at)Octopus(dot)com?
indeed this works well once I’ve upgraded my Octopus 2.6 to 3,2.24