Octopus Deploy Variables Null in PowerShell

I have created several variables to use in my PowerShell scripts (of which there are several), but I can’t get any of them to work. The only variables that are working for me so far are the ones I declare in the script body itself.

For example, my first variable reference is as follows:

Invoke-Command -ComputerName $OctopusParameters[“Octopus.Machine.Hostname”] -ScriptBlock {Stop-WebAppPool -Name $SitecoreSiteName}

The variable - $SitecoreSiteName - is being pulled from the select variable drop-down list at the right, so that leads me to believe that I have it set up correctly. I’m printing out the values with OctopusPrintVariables, and it shows the following correct value:

[SitecoreSiteName] = ‘devint.sc’

However, the task is failing with the following message:

Cannot validate argument on parameter ‘Name’. The argument is null. Provide a valid value for the argument, and then
January 24th 2019 20:10:28Error
try running the command again.
January 24th 2019 20:10:28Error
At E:\Octopus\Work\20190125021025-3038-17\Script.ps1:2 char:1
January 24th 2019 20:10:28Error

  • Invoke-Command -ComputerName $OctopusParameters["Octopus.Machine.Host …
    January 24th 2019 20:10:28Error

January 24th 2019 20:10:28Error
+ CategoryInfo : InvalidData: (:slight_smile: [Stop-WebAppPool], ParameterBindingValidationException
January 24th 2019 20:10:28Error
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.IIs.PowerShell.Provider.StopAppPoolCommand

Any ideas why my variables seem to be null when I reference them from an inline PowerShell script?

Thanks!

Hi Brandon,

Thanks for getting in touch! Sorry for the delay in getting back to you here.

Looking at the information you have provided here, it seems that the SitecoreSiteName variable is configured fine in Octopus. It looks like your script has no value for $SitecoreSiteName when it is being executed.

For Octopus to evaluate a variable in PowerShell it requires the following syntax:
$OctopusParameters["SitecoreSiteName"] or #{SitecoreSiteName}

You can test that your script is receiving a value for this variable by writing it out on the line above the Invoke-Command.

If this does not help, or if you have any further questions here, please let me know. :slight_smile:

Looking forward to hearing from you.

Best regards,
Daniel

Hi Daniel,

I responded to Brandon’s question in the slack channel, but I’ll post it here as well. The problem is with the variables themselves. There is an attempt to pass a local variable from the machine running the script to the scriptblock running on the remote machine and as a result needs to be passed in as an argument to the Invoke-Command cmdlet, like so:

Invoke-Command -ComputerName $OctopusParameters[“Octopus.Machine.Hostname”] -ScriptBlock {Stop-WebAppPool -Name $args[0]} -ArgumentList $SitecoreSiteName

I was able to test this in my environment and it worked like a charm after reproducing Brandon’s issue first.

Thanks,
Garrett

Daniel & Garrett,

Thanks for your responses! I was able to resolve the issue in that particular script using the #{} syntax. However, I’m now receiving another error in my next PowerShell step:

Usage:

$DestinationPath = “#{SolutionArchiveOriginPath}#{SolutionArchiveFilename}”
$TeamCityLatestCheckoutDir = Get-Content -Path “E:\BuildAgent\latest-checkoutdir.txt”
Compress-Archive -Path “$TeamCityLatestCheckoutDir*” -CompressionLevel Optimal -DestinationPath $DestinationPath

Verbose Output:

Parsing file ‘E:\Octopus\Work\20190129192615-3315-58\Script.ps1’ with Octostache returned the following error: The following tokens were unable to be evaluated: '#{SolutionArchiveOriginPath}', '#{SolutionArchiveFilename}'

Error:

Compress-Archive : The path ‘#{SolutionArchiveOriginPath}’ either does not exist or is not a valid file system path.

These variables have been created in the same manner as the previous ones, but they’re not appearing in the OctopusPrintVariables or OctopusPrintEvaluatedVariables output either… any ideas what the issue could be this time?

Thanks,
Brandon

Hi Brandon,

Thanks for the update here! I’m glad that the variable syntax helped here. As for your new issue, if the logs are not outputting the variable whilst the logging variables are added to your deployment then Octopus is not able to resolve them for some reason.

Would you be able to email me the the raw log file with this variable logging enabled? You can send them to support@octopus(dot)com and I will be able to see them.

You said these variable have been created in the same manner as the others, would you be able to provide some more details on how they were created? Are they project variables? Any detailed information you can provide about where these variables are created would help here. Feel free to post the information here, or in the email with the logs.

If you have any further thoughts or questions, please don’t hesitate to let me know.

Best regards,
Daniel

Thank you both so much! Between the two of you, you helped me resolve my issue… my problem was:

  1. The syntax issue. I’ve found that I have to use the #{} syntax in order to utilize project variables.
  2. The issue with local variables ($LocalVariable) being passed in a -ScriptBlock… The feedback provided by @Garrett_Westley was exactly right, and worked to a tee!

Thank you both!

Hi Brandon,

Thanks for the update here! I’m glad to hear that you have this working. :slight_smile:

Please feel free to get back in touch at any time if you have any further thoughts or questions.

Best regard,
Daniel

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.