Link to home
Start Free TrialLog in
Avatar of enthuguy
enthuguyFlag for Australia

asked on

Powershell single quote around variable when passing as argument

Hi Powershell experts,

please help.

Not able to fix below issue. Please note, I'm initiating/sending this cmd from aws cloudformation.

Step 1:
Within cloudformation script/scope, I get a value for RDSInstance.Endpoint.Address, which I'm passing it as a variable to below Step 2. Assume the value is 'newdb.us-east-2.rds.amazonaws.com'

Step 2: Cloudformation script looks like this when invoking
5-replaceDBURLinEachFile:
  command: !Sub |
    powershell.exe foreach($line in Get-Content C:\filepath.txt) {If (Test-Path -Path $line) {(Get-Content $line).replace('cxad1tpfvpbi.us-east-2.rds.amazonaws.com', ${RDSInstance.Endpoint.Address}) | Set-Content $line}}

Open in new window


Step 3: But on the target server it failed, sorry I didnt capture the exact log. but it failed for missing single quote around the variable value.
powershell.exe foreach($line in Get-Content C:\filepath.txt) {If (Test-Path -Path $line) {(Get-Content $line).replace('cxad1tpfvpbi.us-east-2.rds.amazonaws.com', newdb.us-east-2.rds.amazonaws.com) | Set-Content $line}}

Open in new window


How do I place single quote or handle this situation pls

please help, thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of enthuguy

ASKER

Thanks again oBdA :)

Learning from you as well. ;)
That replacement is done by the Cloudformation engine, obviously - it is not PowerShell doing that. So all you should need to do is make sure to include literal single quotes. As I don't know Cloudformation, I cannot tell if just putting single quotes around the variable expression works, or you need to somehow escape them for Cloudformation so it doesn't see them as quotes (and not apply any substitution).
I would start with simple single quotes, like
powershell.exe foreach($line in Get-Content C:\filepath.txt) {If (Test-Path -Path $line) {(Get-Content $line).replace('cxad1tpfvpbi.us-east-2.rds.amazonaws.com', '${RDSInstance.Endpoint.Address}') | Set-Content $line}}

Open in new window

Thanks  so much oBdA. that helped me. :)

Another suggestion pls?
From the above scenario replacing db_url. Is it possible to replace two more variable in single line?
e.g right now i replacce db_url, Can I also replace db_user and db_pwd
Avatar of oBdA
oBdA

The result of a Replace() method is a string, so you can just add as many Replace() as you need
"SomeString".Replace('a', 'b').Replace('c', 'd').Replace('e', 'f')

Open in new window

Really, is PS that powerful? Nice :)

Something like this?
powershell.exe "foreach($line in Get-Content C:\filepath.txt) {If (Test-Path -Path $line) {(Get-Content $line).replace('old_db.rds.amazonaws.com', '${RDSInstance.Endpoint.Address}').replace('old_ueer', '${RDSInstance.user.name}').replace('old_pwd', '${RDSInstance.user.pwd}') | Set-Content $line}}"

Open in new window

That's actually the underlying .NET Framework, but, yes, it's that comfortable.
Be aware that the Replace() method is case sensitive.
Thanks again oBdA!
and thanks Qlemo for your help.