4

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.

5
  • 2
    Don't assign to $args as it's an automatic variable. I've never used that cmdlet before but, I'm pretty sure you're not supposed to use -Variable like 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. Commented Oct 31, 2021 at 0:27
  • Did you mean: $argument = "greeting=" + $hello, "audience=world"; Invoke-Sqlcmd -Query "select '$argumemt'"? Commented Oct 31, 2021 at 0:29
  • Everything in the post is correct; the second version works just fine. Check the Invoke-Sqlcmd docs; I'm doing the exact thing as example 3. Commented Oct 31, 2021 at 0:38
  • Don't use $args as variable name, as Abraham pointed out, it's an automatic PS variable. See $args Commented Oct 31, 2021 at 0:43
  • Ah, gotcha. Good idea, though same thing with a different variable name. I'll update the question to avoid confusion. Commented Oct 31, 2021 at 0:51

1 Answer 1

4

On your first example, the variable $params is being set to string:

$hello = "hello"
$params = "greeting=" + $hello, "audience=world"
$params.GetType()

IsPublic IsSerial Name          BaseType
-------- -------- ----          --------
True     True     String        System.Object

PS /> $params
greeting=hello audience=world

Unless you tell PowerShell you want an object[] as result of your operation. i.e.: surrounding the concatenation operation with ( ):

$params = ("greeting=" + $hello), "audience=world"
$params.GetType()

IsPublic IsSerial Name            BaseType
-------- -------- ----            --------
True     True     Object[]        System.Array

PS /> $params
greeting=hello
audience=world

Or using the array sub-expression operator for example:

$params = @(
    "greeting=" + $hello
    "audience=world"
)

For official documentation on this, see about_Operator_Precedence.

$string = 'a'
$array = 'b','c'

PS /> ($string + $array).GetType()

IsPublic IsSerial Name          BaseType
-------- -------- ----          --------
True     True     String        System.Object

PS /> $string + $array
ab c

PS /> ($array + $string).GetType()

IsPublic IsSerial Name            BaseType
-------- -------- ----            --------
True     True     Object[]        System.Array

PS /> $array + $string
b
c
a
Sign up to request clarification or add additional context in comments.

1 Comment

I could have sworn the types were the same when I checked. Oh, well. Thanks for setting me straight!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.