如何使用PowerShell运行sqlcmd.exe?

时间:2017-06-09 21:17:19

标签: powershell powershell-v4.0

我有一些可以使用cmd.exe运行但现在想在PowerShell中执行此操作的内容。如何重写它以在PowerShell中工作?

#username of db
set username=user
set pwd= password1
#server IP or server name 
set server=test/database1
#database name you are logging into
set dbname=database1
#sql file name
set sqlScript=C:\Scripts\testTables.sql

sqlcmd.exe  -S%server% -U%username% -P%pwd%   -d%dbname% -o"tables.lis"  -i %sqlScript%

1 个答案:

答案 0 :(得分:2)

您可以使用此代码段:

# Path to your sqlcmd.exe
$app = 'sqlcmd.exe'

# Execute the exe with the arguments
& $app -s "DBServerName" -u "YourUsername" -p "YourPassword" -d "DatabaseName" -o "OutputFile" -i "Path to your sql script"