07-21-2023, 09:10 PM
I'm trying to write a powershell script which will execute sqlcmd.exe to run a sql script. The script contains a SQLCMD variable which I want to pass in on the command line via sqlcmd's -v switch. The problem is that powershell is doing something weird when I pass a path as the value of one of my variables which in turn causes the SQL script to fail.
For example I'm calling:
$path = 'C:\path'
sqlcmd -SMySQLServerInstance -i 'MySqlScript.sql' -v MyVariablePath=$path
when run I receive a error which says:
Sqlcmd: ':\path': Invalid argument.
No amount of double or single quotes that I have tried around `$path` or `MyVariablePath=$path`
solves the issue.
Can somebody provide a simple canonical example of how this needs to be done?
For example I'm calling:
$path = 'C:\path'
sqlcmd -SMySQLServerInstance -i 'MySqlScript.sql' -v MyVariablePath=$path
when run I receive a error which says:
Sqlcmd: ':\path': Invalid argument.
No amount of double or single quotes that I have tried around `$path` or `MyVariablePath=$path`
solves the issue.
Can somebody provide a simple canonical example of how this needs to be done?