Invoke-Sqlcmd : The type initializer for 'Microsoft.Data.SqlClient.InOutOfProcHelper' threw an exception

2.3k Views Asked by At

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
1

There are 1 best solutions below

0
fred727 On

For me, it was because the version of the module installed was certainly buggy.

Install-Module sqlserver installed the 21.1.1 ; I had to uninstall it and reinstall the 21.1.18256 :

Uninstall-Module -Name SqlServer
Install-Module SqlServer -RequiredVersion 21.1.18256 -Force

All is OK now...

(to know the actual version of your installed modules, use Get-InstalledModule)