Powershell help - variables

Soldato
Joined
25 Nov 2004
Posts
3,792
Hi all

Let me start by saying I am by no means a Powershell anything, learning as I go and mostly copying other peoples work and trying to learn from it. Basically I have been thrust into a DevOps role at work and trying to get things done. So lots of VSTS, CICD and Release Management stuff.

Anyway, I have a web application in which I need to do some web.config XML variable substitution. This is handled by VSTS release tasks usually but the task only looks within appsettings and connectionstring sections to substitute variables and the one I need sits outside of these areas. So I found some powershell that will simply replace the entire line with the correct settings. So it looks like this:

$NetworkHost = "<network host="smtp.sendgrid.net" port="587" defaultCredentials="false" enableSsl="true" userName="[email protected]" password="Azure" />"

(get-content "C:\inetpub\webroot\website\web.config") | foreach-object {$_ -replace "<network host="smtp.gmail.com" port="587" defaultCredentials="false" enableSsl="true" userName="[email protected]" password="Gmail" />", "$NetworkHost"} | set-content "C:\inetpub\webroot\website\web.config"

It doesn't work and I know why, I just don't know what to do about it. It is reading the variable as:

$NetworkHost = "<network host="

and the rest as another command or a switch or what have you. How can I tell it that the entire thing is the actual variable? I tried double quote marks, it didn't help.

Thanks in advance for any help! If there is a better way to handle this I am welcome to suggestions too.

EDIT: actually it's reading both "<network host=" as the only part of the "string" (if that's the right word) as the actual error is:

Unexpected token 'smtp.gmail.com" etc
Unexpected token 'smtp.sendgrid.net" etc
 
Soldato
OP
Joined
25 Nov 2004
Posts
3,792
Alternatively you could avoid the problem entirely by delimiting the string itself with single quotes:

Code:
$NetworkHost = '<network host="smtp.sendgrid.net" port="587" defaultCredentials="false" enableSsl="true" userName="[email protected]" password="Azure" />'

Gah! I did this, and it didn't work! Now it does. Typical. Thank you very much for your help :)
 
Back
Top Bottom