如何从 PowerShell 绑定 sqlcmd 的 -v 参数 [英] How to bind the -v parameter of sqlcmd from a PowerShell

查看:24
本文介绍了如何从 PowerShell 绑定 sqlcmd 的 -v 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码,直接设置-v参数即可

The following code, setting the -v parameter directly is working

$sqlcmd = @(Resolve-Path "$env:ProgramFiles\Microsoft SQL Server\*\Tools\binn\SQLCMD.EXE")[0]

$path1 = 'D:\somescript.sql'

& $sqlcmd -b -S NB-BK\SQLEXPRESS -d BK_Prod -U sa -P mypassword -l 180 -i $path1 -v Mandant=1 SAPMandant="009" SAPEinrichtung="0001" 

但我需要一种方法来从 PowerShell 变量设置这些值.

But I need a way to set these values from a PowerShell variable.

我试过了:

$sqlcmd = @(Resolve-Path "$env:ProgramFiles\Microsoft SQL Server\*\Tools\binn\SQLCMD.EXE")[0]

$path1 = 'D:\somescript.sql'

$sqlcmdparameters = 'Mandant=1 SAPMandant="009" SAPEinrichtung="0001" '
& $sqlcmd -b -S NB-BK\SQLEXPRESS -d BK_Prod -U sa -P mypassword -l 180 -i $path1 -v $sqlcmdparameters

我在上发现了这个,但对我没有帮助.

I found this on SO, but it didn't help me.

推荐答案

尝试使用 start-process.这就是我为 Powershell 命令行解析问题所做的.首先尝试调用运算符 (&),如果这不起作用,请将其包装在启动进程调用中.

Try using start-process. That's what I do for Powershell command-line parsing issues. First try invoke operator (&) and if that doesn't work wrap it in a start-process call.

$tempFile = [io.path]::GetTempFileName()

$exitCode = (start-process -FilePath $sqlcmd -ArgumentList @"
-b -S NB-BK\SQLEXPRESS -d BK_Prod -U sa -P mypassword -l 180 -i $path1 -v $sqlcmdparameters
"@ -Wait -RedirectStandardOutput $tempFile -NoNewWindow -Passthru).ExitCode

通常在使用 start-process 时,您需要捕获退出代码并将输出重定向到临时文件以获取结果.然后我将有一些代码来检查 $exitcode 并清理 try/catch/finally 块中的临时文件

When using start-process typically you'll need to capture exitcode and also redirect output to temp file in get back the results. I'll then have some code to check $exitcode and cleanup temp file in try/catch/finally block

这篇关于如何从 PowerShell 绑定 sqlcmd 的 -v 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆