mithrandyr / SimplySql

PowerShell module for querying various SQL databases
MIT License
197 stars 31 forks source link

SimplySQL on Windows 2022 - Powershell 5.1 requires repeated re-installation before each use #162

Open bab5470 opened 1 month ago

bab5470 commented 1 month ago

I am trying to use SimplySQL in a Powershell script on our Windows Server 2022 system with Powershell 5.1. If I run Install-Module SimplySQL and Import-Module Simply SQL everything works as expected.

However, if I close my Powershell window and attempt to re-use it I get this error:

writeErrorStream      : True
Exception             : System.TypeInitializationException: The type initializer for
                        'MySqlConnector.Utilities.ActivitySourceHelper' threw an exception. --->
                        System.TypeLoadException: Could not load type 'System.Diagnostics.Metrics.Meter' from assembly
                        'System.Diagnostics.DiagnosticSource, Version=5.0.0.0, Culture=neutral,
                        PublicKeyToken=cc7b13ffcd2ddd51'.
                           at MySqlConnector.Utilities.ActivitySourceHelper..cctor()
                           --- End of inner exception stack trace ---
                           at MySqlConnector.Utilities.ActivitySourceHelper.StartActivity(String name, IEnumerable`1
                        activityTags)
                           at MySqlConnector.MySqlConnection.<OpenAsync>d__29.MoveNext()
                        --- End of stack trace from previous location where exception was thrown ---
                           at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                           at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
                        task)
                           at MySqlConnector.MySqlConnection.Open()
                           at SimplySql.Engine.Logic.OpenAndAddConnection(baseConnectionDetail connDetail)
                           at SimplySql.Cmdlets.OpenMySqlConnection.ProcessRecord()
TargetObject          : default
CategoryInfo          : OpenError: (default:String) [Open-MySqlConnection], TypeInitializationException
FullyQualifiedErrorId : OpenMySQLConnection.Error,SimplySql.Cmdlets.OpenMySqlConnection
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, C:\ptp\scripts\SprintPrepSyncWordpressDatabases.ps1: line 170
                        at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {0, 1}
PSMessageDetails      :

If I do an uninstall-module simplysql then an install-module simplysql and an import-module simplysql again it resumes working. So something with the module is getting messed up. It seems like it is related to 'System.Diagnostics.Metrics.Meter' from assembly 'System.Diagnostics.DiagnosticSource, Version=5.0.0.0, Culture=neutral,

I don't have anything other than Windows Defender antivirus installed and no other security applications I can think of that might be tampering with the installation. I have other modules I am using that do not have this issue - it's only simplysql. Has anyone else seen this issue?

$PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      20348  2400
> Get-Module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Con...
Manifest   3.0.0.0    Microsoft.PowerShell.Security       {ConvertFrom-SecureString, ConvertTo-SecureString, Get-Acl...
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Script     2.0.0      PSReadline                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PS...
Binary     2.0.4.75   SimplySql                           {Clear-SqlMessage, Close-SqlConnection, Complete-SqlTransa...
Script     22.1.1     SqlServer                           {Add-RoleMember, Add-SqlAvailabilityDatabase, Add-SqlAvail...

I do have the SQLServer Powershell module installed - maybe that's conflicting? Although it seems to only by simplysql that's the problem.

bab5470 commented 1 month ago

I see System.Diagnostics.DiagnosticSource.dll in the C:\Program Files\WindowsPowerShell\Modules\SimplySql\2.0.4.75\bin file version 8.0.23.53103

mithrandyr commented 3 weeks ago

@bab5470 -- try launching powershell clean powershell -noprofile and the import SimplySql Import-Module SimplySql then verify that only this module is loaded (i.e. that SqlServer is not) Get-Module.