I am trying to pass in some scripting variables to Invoke-Sqlcmd in PowerShell like so:
$hello = "hello"
$params = "greeting=" + $hello, "audience=world"
Invoke-Sqlcmd -Query "select '`$(greeting) `$(audience)'" -Variable $params
I get the following error:
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.
But I am successful if I remove $hello and use a literal:
$params = "greeting=hello", "audience=world"
.GetType() returns the same thing for both versions of $params, so I'm unsure what the issue is.
$argsas it's an automatic variable. I've never used that cmdlet before but, I'm pretty sure you're not supposed to use-Variablelike that. Also, what's your intentions, 'cause there is no actual variable in your string, so there is no need to use a sub-expression, nor to escape it using the back tick.$argument = "greeting=" + $hello, "audience=world"; Invoke-Sqlcmd -Query "select '$argumemt'"?$argsas variable name, as Abraham pointed out, it's an automatic PS variable. See$args