2

I do apologies if this has been asked before. At the time I wrote this I was unable to find this behavior addressed on this site.

When using Invoke-SQLcmd in powershell I get inconsistencies. I receive SQL connection arguments at the scripts invocation from the user. Inside of the this single test script I have two queries. Both invocations use the same variables.

This works and returns the name of the database.

$ConnectQuery = "SELECT DBName = DB_NAME()"

Invoke-Sqlcmd -ServerInstance $SQLServer -Database $SQLDBName -Username $SQLUsername -Password $SQLPassword -Query $ConnectQuery

This does not work but does not give me an error but returns nothing. I checked the variables and they continue to have the same content as that this query immediately follows the previous.

$ConnectQuery01 = "SELECT * FROM [dbo].[test.csv]"
    
Invoke-Sqlcmd -ServerInstance $SQLServer -Database $SQLDBName -Username $SQLUsername -Password $SQLPassword -Query $ConnectQuery01

Additional Notes:

As I understand Invoke-Sqlcmd it is a module that provides Powershell access to the same features as the command shell command "sqlcmd". So I tried this using "sqlcmd". In both cases, these commands worked and return the expected data The databases name, and the table content which is a single test row.

In SQLcmd executable:

These both work and return the expected data.

sqlcmd -S <MyDesktopName> -d test -U "<TestAccountName>" -P "<TestPassword>" -q "SELECT DBName = DB_NAME()"
sqlcmd -S <MyDesktopName> -d test -U "<TestAccountName>" -P "<TestPassword>" -q "SELECT * FROM [dbo].[test.csv]"

In SSMS:

These both work and return the expected data.

SELECT DBName = DB_NAME()
SELECT * FROM [dbo].[test.csv]

Just in case someone asks

$PSVersionTable

Name                           Value                                                                                                                         
----                           -----                                                                                                                         
PSVersion                      5.1.18362.1171                                                                                                                
PSEdition                      Desktop                                                                                                                       
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                       
BuildVersion                   10.0.18362.1171                                                                                                               
CLRVersion                     4.0.30319.42000                                                                                                               
WSManStackVersion              3.0                                                                                                                           
PSRemotingProtocolVersion      2.3    
SerializationVersion           1.1.0.1   
3
  • 1
    Are you sure there's data in [dbo].[test.csv] If you use SSMS to query the table as the same user, do you get rows? Commented Nov 30, 2020 at 9:18
  • Yes I use SSMS and SQLcmd.exe and both return the single row of data that I inserted in that table. Commented Nov 30, 2020 at 15:58
  • Ok I think I have found a way to mitigate this behavior. If I do this:$MyResult = Invoke-Sqlcmd -ServerInstance $SQLServer -Database $SQLDBName -Username $SQLUsername -Password $SQLPassword -Query $ConnectQuery01 Commented Dec 2, 2020 at 20:02

1 Answer 1

0

Ok I think I have found a way to mitigate this behavior.

If I do this:

$ConnectQuery01 = "SELECT * FROM [dbo].[test.csv]"

$MyResult = Invoke-Sqlcmd -ServerInstance $SQLServer -Database $SQLDBName -Username $SQLUsername -Password $SQLPassword -Query $ConnectQuery01

Then I do this:

Write-Host $MyResult. | Format-Table

I get the table results that I expect. This is significantly different behavior from the first evocation of "Invoke-sqlcmd" So, I believe that there are different return behaviors depending on the expected return. I could be wrong here. Please let me know if you have a better explanation.

Thank you for you attention.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.