0

If I run:

<somecommand> -e "SHOW DATABASES" -u<user> -p<password>

It runs. But if I try:

<somecommand> -e "SHOW DATABASES" -u$sqlUser -p$sqlPassword

it fails because the variables are not being expanded.

What is the simplest way to accomplish this task in powershell?

David Rogers
  • 83
  • 2
  • 6

1 Answers1

1

Assuming somecommand is an exe (or DOC command as you state), try it this way:

<somecommand> -e "SHOW DATABASES" "-u$sqlUser" "-p$sqlPassword"
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • I think I tried 4095 other things.... But this did indeed work! Thanks for your response! – David Rogers Apr 12 '14 at 04:03
  • PowerShell would use argument parsing mode in this case which means it tries to let you specify string arguments without having to use quotes (which would get old quickly). It is smart about seeing -u$sqlUser and it would expand the variable in this case however -u$sqlUser it sees as a string. You can force PowerShell to expand variables by putting double quotes around it (and the adjoining text). – Keith Hill Apr 12 '14 at 04:05
  • Yeah, sure that will work also `-u"$sqlUser"`. When passing to an exe whether its `"-u$sqlUser"` or the previous way, doesn't matter AFAICT. – Keith Hill Apr 12 '14 at 04:07