Pregunta

In my build script, I have a helper powershell function as below:

function set-connectionstring {
  param($path, $name, $value)
  $settings = [xml](get-content $path)
  $setting = $settings.configuration.connectionStrings.add | where { $_.name -eq $name }
  $setting.connectionString = "$value"
  $setting.providerName = "System.Data.SqlClient"
  $resolvedPath = resolve-path($path) 
  $settings.save($resolvedPath)
}

It works great except that when I try to set the value, it encodes ampersands weirdly. Here is the issue:

Inside my connection string I have &quot value. This should be perfectly valid. However, my above code transforms this into " which is totally unreasonable. Do you have any idea I can solve this problem?

¿Fue útil?

Solución

Have you try to decode your connection string first? Here is sample:

[System.Reflection.Assembly]::LoadWithPartialName("System.web")
[System.Web.HttpUtility]::HtmlDecode(""")

Otros consejos

You are sticking $value in the middle of a string literal, so it is behaving as though you had typed: $setting.connectionString = ""something"". This is not an XML operation, it is a .NET string assignment, so anything on the right hand side of the equal needs to be literally what you want in the connection string, not the escaped XML attribute representation. So either pass it in unescaped, or unescape it before the assignment.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top