Vaccano Vaccano - 3 months ago 7
PowerShell Question

Use a variable in a command

This is part of the PowerShell script I am working on:

Write-Host $configJson.myVal
(Get-Content .\config.js) -replace "S=''", "S='$configJson.myVal';" | Set-Content .\out.js


The
Write-Host
part correctly displays the value in
$configJson.myVal
.

But when I run the second statement, the value that is put in the file is:
System.Collections.Generic.Dictionary'2[System.String,System.Object].deployedBaseUrl


How can I change the second command so that the value that is output on the
Write-Host
line is also put into the file for my replace command?

Answer

I would use a format string:

Write-Host $configJson.myVal
(Get-Content .\config.js) -replace "S=''", ("S='{0}';" -f $configJson.myVal) | Set-Content .\out.js
Comments