Powershell将参数传递给sqlcmd

时间:2017-01-14 17:38:07

标签: powershell sqlcmd

我试图将username,db和pwd作为变量传递给sqlcmd - 但它似乎不喜欢它。

这有效

#Run SQLCMD for each file
ForEach ($FileName in $FileNames)
{
    Write-Host $FileName.BaseName
    sqlcmd -S 123.database.net -d EMR -U user1-P mypassword -i $FileName.FullName
}

这不起作用

#Run SQLCMD for each file
$db = "123.database.net"
$dbId = "user1"
$pwd = "mypassword"

ForEach ($FileName in $FileNames)
{
    Write-Host $FileName.BaseName
    sqlcmd -S $db -d EMR -U $dbId -P $pwd -i $FileName.FullName
}

错误消息

  

sqlcmd:Sqlcmd:错误:SQL Server的Microsoft ODBC驱动程序13:用户登录失败' user1' ..

1 个答案:

答案 0 :(得分:0)

sqlcmd有其范围。将变量设为Global。然后它应该选择它。

做到这一点:

#Run SQLCMD for each file
$global:db = "123.database.net"
$global:dbId = "user1"
$global:pwd = "mypassword"
ForEach ($FileName in $FileNames)
{

Write-Host $FileName.BaseName
sqlcmd -S $db -d EMR -U $dbId -P $pwd -i $FileName.FullName

}

注意:我没有考虑到你的第二个小脚本是否会抛出任何错误。