-1

I'm trying to pass username, db and pwd as variable to sqlcmd - but it does not seem to like it.

This works

#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
}

This does not work

#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
}

Error message

sqlcmd : Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login failed for user 'user1'..

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Justin Homes
  • 3,739
  • 9
  • 49
  • 78
  • What does "does not work" mean? What error message(s) do you get? Have you output the command line being executed to check that it's correct? – alroc Jan 14 '17 at 17:42

1 Answers1

0

sqlcmd has its scope .Make the variables as Global. Then it should pick it.

DO this:

#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

}

Note: I have not taken into consideration that your second scriptlet is throwing any error in particular or not.

Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45