简体   繁体   English

批处理脚本中的powershell命令invoke-sqlcmd

[英]powershell command invoke-sqlcmd in batch script

I have a powershell command that I want to be run in batch script. 我有一个Powershell命令,希望在批处理脚本中运行。 It works well in powershell window but I cannot call it properly in batch script. 它在Powershell窗口中运行良好,但是我无法在批处理脚本中正确调用它。 the powershell command goes like this: powershell命令如下所示:

invoke-sqlcmd -inputfile "D:\Reports\sql.sql" -ServerInstance SERVER1 | export-csv "D:\Rpt\historical\sample1.csv" -Force -En UTF8

I hope somebody could help me out. 我希望有人能帮助我。 Also, is it possible to include batch variable in replace of the file path for input and output file instead of putting the whole path in powershell command (stil run inside in batch script)? 另外,是否可以在输入和输出文件的文件路径中包含批处理变量,而不是将整个路径放在powershell命令中(在批处理脚本中在内部运行)?

Thanks. 谢谢。

I have found that works :) to run the invoke-sqlcmd within batch script: 我发现可以运行:)在批处理脚本中运行invoke-sqlcmd:

I have use this line: 我已经使用了这一行:

powershell -Command "& {Add-PSSnapin SqlServerCmdletSnapin100; Add-PSSnapin SqlServerProviderSnapin100; invoke-sqlcmd -inputfile '%sqlPath1%' -ServerInstance %Server% | export-csv '%out_path1%\\%out1_fn%' -Force -En UTF8;}"

works like charm yay! 就像魅力耶! ^_^ ^ _ ^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM