我需要使用 bcp 从远程 SQL 数据库中提取并保存一些表。我想编写一个 powershell 脚本来为每个表调用 bcp 并保存数据。到目前为止,我有这个脚本可以为 bcp 创建必要的参数。但是我不知道如何将参数传递给 bcp。每次我运行脚本时,它只会显示 bcp 帮助。这一定是我没有得到的非常容易的事情。
#commands bcp database.dbo.tablename out c:\temp\users.txt -N -t, -U uname -P pwd -S <servername>
$bcp_path = "C:\Program Files\Microsoft SQL Server\90\Tools\Binn\bcp.exe"
$serverinfo =@{}
$serverinfo.add('table','database.dbo.tablename')
$serverinfo.add('uid','uname')
$serverinfo.add('pwd','pwd')
$serverinfo.add('server','servername')
$out_path= "c:\Temp\db\"
$args = "$($serverinfo['table']) out $($out_path)test.dat -N -t, -U $($serverinfo['uid']) -P $($serverinfo['pwd']) -S $($serverinfo['server'])"
#this is the part I can't figure out
& $bcp_path $args