Sep 15 2023 08:42 AM
Hello
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 192.168.0.4 -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 192.168.0.4 -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
errors...
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 192.168.0.4 -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 192.168.0.4 -Username "PAReadonly2" -Password " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Invoke-Sqlcmd], ParserException
+ FullyQualifiedErrorId : ExecutionFailureException,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
Thanks
Sep 15 2023 11:50 AM