Prakash
Prakash

Reputation: 27

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

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

Upvotes: 2

Views: 3843

Answers (1)

fred727
fred727

Reputation: 2854

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

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

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

The command is working now.

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

Upvotes: 1

Related Questions