Difficulties working with powershell Invoke-SQLCmd

powershellsqlcmd

I've setup a simple sqlcmd script. Contents below.

  use $(dbname);
  select * from $(tablename)

I'm then running the following Invoke SQLCMD

$SQLCMDVarArr = "dbname='MyDatabase'", "Tablename='MyTable'"

INVOKE-SQLCMD -ServerInstance MyServer\MyInstance -inputFile '\\MyShare\Database\Sprints\csrtemp2\csrtest.sql' -variable $SQLCMDVarArr

And I'm getting the error:

  INVOKE-SQLCMD : Incorrect syntax near 'MyDatabase'.

I pulled the syntax straight from the invoke-sqlcmd get-help examples. Can anyone tell me what is wrong with this?

Thanks,

as per request this is running on windows Server 2008 R2

Best Answer

So it turns out that the get-help invoke-sqlcmd -example shows the array variable as using 'dbname="MyDatabase"' as the syntax. However this gives the error above but if you swap the quote positions such that the variable uses "dbname='MyDatabase'" then the script works.