enthuguy
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.Addre ss, which I'm passing it as a variable to below Step 2. Assume the value is 'newdb.us-east-2.rds.amazo naws.com'
Step 2: Cloudformation script looks like this when invoking
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.
How do I place single quote or handle this situation pls
please help, thanks in advance
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.Addre
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}}
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}}
How do I place single quote or handle this situation pls
please help, thanks in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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}}
ASKER
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
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
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')
ASKER
Really, is PS that powerful? Nice :)
Something like this?
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}}"
That's actually the underlying .NET Framework, but, yes, it's that comfortable.
Be aware that the Replace() method is case sensitive.
Be aware that the Replace() method is case sensitive.
ASKER
Thanks again oBdA!
and thanks Qlemo for your help.
and thanks Qlemo for your help.
ASKER
Learning from you as well. ;)