Variables without spaces at the end

I’m trying to write a very simple find/replace script in PowerShell but it needs to utilize some $OctopusParameters. Whenever there is not a space at the end of the parameter it doesnt’ appear to be resolved

Write-Output "- > Path: " $OctopusParameters['Octopus.Action[Deploy MyApp Admin].Package.CustomInstallationDirectory']
(Get-Content $OctopusParameters['Octopus.Action[Deploy MyApp Admin].Package.CustomInstallationDirectory']\wwwroot\app\js\app.js) | 
Foreach-Object {$_ -replace "MyApp .local", "beta.MyApp.com"} | 
Set-Content $OctopusParameters['Octopus.Action[Deploy MyApp Admin].Package.CustomInstallationDirectory']\wwwroot\app\js\app.js

The problem is in the Write-Output the $OctopusParameters is inserted correctly
In the Get-Content the $OctopusParameters is inserted as a literal instead of evaluated because it has a \ at the end.

What I’m trying to do is build a path with the Octopus parameter being the first part.

Any suggestions would be apprechiated!

Hi Mitch,

Thanks for getting in touch!

In Powershell, if a variable is going to be only part of a string argument, you need to wrap it in quotes. However, the easiest way to get this to work would be to set a variable that contains the Octopus variable and use that in your Set-Content command. So for your script, try:

Write-Output "- > Path: " $OctopusParameters['Octopus.Action[Deploy MyApp Admin].Package.CustomInstallationDirectory']
$appJsPath = $OctopusParameters['Octopus.Action[Deploy MyApp Admin].Package.CustomInstallationDirectory']
(Get-Content "$appJsPath\wwwroot\app\js\app.js") | 
Foreach-Object {$_ -replace "MyApp .local", "beta.MyApp.com"} | 
Set-Content "$appJsPath\wwwroot\app\js\app.js"

Hope that helps!

Damo