Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从PowerShell绑定sqlcmd的-v参数_Powershell_Sqlcmd - Fatal编程技术网

如何从PowerShell绑定sqlcmd的-v参数

如何从PowerShell绑定sqlcmd的-v参数,powershell,sqlcmd,Powershell,Sqlcmd,下面的代码,直接设置-v参数正在工作 $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 SAPManda

下面的代码,直接设置-v参数正在工作

$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变量设置这些值的方法

我试过:

$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

我找到了这个,但它对我没有帮助。

尝试使用启动过程。这就是我为Powershell命令行解析问题所做的。首先尝试调用操作符(&),如果不起作用,则将其包装在启动流程调用中

$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块中的$exitcode和cleanup temp文件。这就是我为Powershell命令行解析问题所做的。首先尝试调用操作符(&),如果不起作用,则将其包装在启动流程调用中

$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 block中的$exitcode和cleanup temp文件

我找到了PowerShell V2的以下解决方案

$sqlcmd = @(Resolve-Path "$env:ProgramFiles\Microsoft SQL Server\*\Tools\binn\SQLCMD.EXE")[0]
$path1 = 'D:\somescript.sql'
$cmdparameters = @(
    'Mandant=1',
    'SAPMandant="009"'
)

& $sqlcmd -b -S NB-BK\SQLEXPRESS -d BK_Prod -U sa -P mypassword -l 180 -i $path1 -v $cmdparameters

我为PowerShell V2找到了以下解决方案

$sqlcmd = @(Resolve-Path "$env:ProgramFiles\Microsoft SQL Server\*\Tools\binn\SQLCMD.EXE")[0]
$path1 = 'D:\somescript.sql'
$cmdparameters = @(
    'Mandant=1',
    'SAPMandant="009"'
)

& $sqlcmd -b -S NB-BK\SQLEXPRESS -d BK_Prod -U sa -P mypassword -l 180 -i $path1 -v $cmdparameters

+1以获得有效的解决方案。我接受你的答案,因为它展示了如何在所有参数映射中使用字符串。我找到了解决这个问题的更直接的方法。请参阅我的答案。+1表示有效的解决方案。我接受你的答案,因为它展示了如何在所有参数映射中使用字符串。我找到了解决这个问题的更直接的方法。看我的答案。有趣的是传递数组是有效的。在V1/V2 get中调用旧命令在Powershell V3中使用-%运算符会更好。Keith Hill在博客中发布了使用V3的-%operator的sqlcmd-v示例:传递数组很有趣。在V1/V2 get中调用旧命令在Powershell V3中使用-%运算符会更好。Keith Hill发表了一篇使用V3的-%运算符的sqlcmd-v示例博文: