I have to call bcp.exe from a PowerShell script. My code is:
$dataBase = 'MyDb'
$ProdEinheitTable = 'dbo.ProdEinheit'
$ProzessdatenTable = 'dbo.Prozessdaten_aktuell'
$sqlServerUserName = 'sa'
$sqlServerPassword = 'Password'
$server = 'MSSQLLocalDB'
$bcp = & 'C:\Program Files\Microsoft SQL Server\110\Tools\Binn\bcp.exe'
and I am calling the bcp utility like this:
$bcp_args = "$bcp $dataBase.$ProdEinheitTable IN $datFileName -f $fmtFileName -U $sqlServerUserName -P sqlServerPassword -S $server -n"
Invoke-Expression $bcp_args
It gives me the error
usage: : The term 'usage:' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At line:1 char:1
+ usage: C:\Program Files\Microsoft SQL Server\110\Tools\Binn\bcp.exe { ...
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (usage::String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
If I remove & from the string I get the exception
C:\Program : The term 'C:\Program' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ C:\Program Files\Microsoft SQL Server\110\Tools\Binn\bcp.exe MyDb.dbo ...
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Program:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
How can I call bcp from PowerShell?
-ArgumentListparameter. - PatrickInvoke-Expression. - Ansgar Wiechers