Search code examples
sql-serverpowershellxp-cmdshell

How do I escape the "&" symbol in Sql server


I have to execute the following code in query editor

Exec xp_cmdshell 'sqlps -command "$http=New-Object system.Net.WebClient;$http.uploadString(\"http://192.168.2.3:8080/ThermalMap/DBTest.jsp\",\"param1=somevalue & param2=thriu\")"'

It gives me error.

But the following code is working well and gives me output

Exec xp_cmdshell 'sqlps -command "$http=New-Object system.Net.WebClient;$http.uploadString(\"http://192.168.2.3:8080/ThermalMap/DBTest.jsp\",\"param1=somevalue\")"'

Here in second code am passing single parameter and I don't get any error If I pass two parameter I need to add "&" symbol and am getting error. How can I escape "&" symbol here?


Solution

  • According to this article you need to escape ampersands like this: ^&. Unescaped ampersands are treated as a command separators.