5

I am trying to run a sql query from powershell for Windows SQL Server 2008 R2. It looks like the code is failing to pass SQL Server Authentication credentials to run the sqlcmd correctly. I am running this first part directly from powershell, not as a script.

Set-Location SQLServer:\SQL\Server\
Invoke-sqlcmd "SELECT DB_NAME() AS hist;" -username "username" -password "p@ss"

I receive error, Login failed for user 'username'. The ideal end result for the code format would look more like this. Same resulting error.

$path = "sqldbdump.csv"
$server = "serveronlocalhost"
$db = "hist"
$Query = @"
SELECT * From alarms;
"@
Invoke-Sqlcmd -ServerInstance $server -Database $db -Username $username -Password $pw -Query $Query | Export-CSV $path

I have also tried convert-tosecurestring -asplaintext with no success.

$pw = convertto-securestring -AsPlainText -Force -String "p@ss"
asked Mar 11, 2014 at 20:13
5
  • If you run Sqlcmd from a command prompt with the -U username -P p@ss parameters can you connect to the SQL Server? Commented Mar 11, 2014 at 20:32
  • Same error, Login failed for user 'username'. Commented Mar 11, 2014 at 20:51
  • Have you verified that you have the username & password correct? Commented Mar 11, 2014 at 21:11
  • Yes, they work when I log in through SQL Server Management Studio Commented Mar 11, 2014 at 21:21
  • Do you have any firewalls on? or blocking port 1433? For ex. to confirm, log in through SQL Server Management Studio from your workstation, not from the server. Commented Mar 11, 2014 at 22:02

2 Answers 2

10

Never use double quotes for a variable containing a password because special characters are interpreted (the same for username containing special characters). It is not the case with single quoted strings.

For example, this password will cause an authentification error because the character $ is evaluated:

$pw = "p@ss$ord!"

But if you use single quotes, it will work:

$pw = 'p@ss$ord!'

Please note that the same principle applies to command lines, so this wil not work:

Invoke-Sqlcmd ... -Password "p@ss$ord!"

You should write:

Invoke-Sqlcmd ... -Password 'p@ss$ord!'
answered Aug 25, 2016 at 11:37
Sign up to request clarification or add additional context in comments.

Comments

0

For everyone that might experience this issue in the future. Run your Invoke-SqlCmd directly through the PowerShell terminal and not through a Text Editor like vsCode. If it works, change the encoding of your script from UTF-8 to UTF-8 with BOM.

Some special characters are getting messed up when used in UTF-8 format, that's why you need to use UTF-8 with BOM.

answered Mar 20, 2023 at 14:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.