Monday, January 6, 2014

invoke-expression for sqlcmd from powershell

Having some issues running a sqlcmd via powershell on a sql instance with custom ports. Try the following command.

Does not work:
sqlcmd -S USHXXDBWD002\I0007DS01,4070 -E -Q "select getdate()"

Does work:
$query = "select getdate()"
$cmd = "SQLCMD -S `"USHGNDBWD002\I0007DS01,4070`" -h-1 -Q `"$query`""
write-host "command is: $cmd"
invoke-expression $cmd

So, when calling SQLCMD directly, quotes were not necessary around the ; however, when invoking SQLCMD from PowerShell using Invoke-Expresion, the multi-part identifier needed quotes.


Post a Comment