Authentication errors when using Invoke-SQLCMD

Copper Contributor


I have been using the below batch file to export some data out of SQL using this SQLcmd ....a (for obvious reasons passwords are changed but that is all that has changed).  This works fine but because of the data format that is outputted, I need to edit it before I can use it.


sqlcmd -S -U PAReadonly2 -P testpw -i "C:\Users\Amber\Documents\SQL Server Management


It was suggested that I do this in PowerShell so with the help in this community I have converted the batch script to PowerShell.   Finally I got the PowerShell side working but as you see below I am now getting authentication errors and there is nothing wrong with the creds  because it works fine in the batch file. 


 This is a local SQL account defined in SQL server itself and there is no domain involved


Invoke-Sqlcmd -Server -Username "PAReadonly2" -Password "testpw" -Database "lc_V3_acephoto_net" -InputFile "C:\Users\Amber\Documents\SQL Server Management Studio\MPNsimple.sql" |
        ConvertTo-Csv -NoTypeInformation |
        Select-Object -Skip 1 |
            ForEach-Object { $_.Replace("""", "") } |
                 Out-File -FilePath "c:\junk\test.csv";


Any help or guidance to make this script work would be really GREAT




Invoke-Sqlcmd : Cannot open database "lc_V3_acephoto_net" requested by the login. The login failed.
Login failed for user 'PAReadOnly2'.
At line:1 char:1
+ Invoke-Sqlcmd -Server -Username "PAReadonly2" -Password " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlException
+ FullyQualifiedErrorId : SqlExectionError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

Invoke-Sqlcmd :
At line:1 char:1
+ Invoke-Sqlcmd -Server -Username "PAReadonly2" -Password " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Invoke-Sqlcmd], ParserException
+ FullyQualifiedErrorId : ExecutionFailureException,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand



1 Reply

I am sorry... Please ignore this. My service provider changed the password on me yesterday and just told me about it...

I am sooooo sorry.