I'm trying to execute SQL Query from Powershell.
My Script:
Invoke-Sqlcmd -Query "select 'Y' from sys.databases" -ServerInstance .
Error :
Invoke-Sqlcmd : The type initializer for 'Microsoft.Data.SqlClient.InOutOfProcHelper' threw an exception.
At C:\temp\Untitled5.ps1:2 char:1
+ Invoke-Sqlcmd -Query "select 'Y' from sys.databases" -ServerInstance ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidResult: (.:PSObject) [Invoke-Sqlcmd], TypeInitializationException
+ FullyQualifiedErrorId : ExecutionFailed,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
For me, it was because the version of the module installed was certainly buggy.
Install-Module sqlserverinstalled the 21.1.1 ; I had to uninstall it and reinstall the 21.1.18256 :All is OK now...
(to know the actual version of your installed modules, use
Get-InstalledModule)