Invoke-Sqlcmd variable format is invalid with Powershell, variable value contains equals sign

28 Views Asked by At

I took an example from the document to add the variable of Invoke-Sqlcmd

$StringArray = "MYVAR1='String1'", "MYVAR2='String2'"
Invoke-Sqlcmd -Query "SELECT `$(MYVAR1) AS Var1, `$(MYVAR2) AS Var2" -Variable $StringArray

Var1     Var2
----     ----
String1  String2

But the value of the variable cannot have an equal sign, here is an example

$StringArray = "MYVAR1='String1'", "MYVAR2='String2=String3'"
Invoke-Sqlcmd -Query "SELECT `$(MYVAR1) AS Var1, `$(MYVAR2) AS Var2" -Variable $StringArray

I got error: Invoke-Sqlcmd : The format used to define the new variable for Invoke-Sqlcmd cmdlet is invalid. Please use the 'var=value' format for defining a new variable.

Is there any way to bypass that.

0

There are 0 best solutions below