mithrandyr / SimplySql

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

Update Classes.ps1 #45

Closed joalcorn closed 2 years ago

joalcorn commented 3 years ago

Using New-Object with the string type specified in the property instantiation for $Messages caused errors when working in PowerShell.exe and not in the ISE. The error avoided is:

New-Object : Cannot find type [System.Collections.Generic.Queue[SqlMessage]]: verify that the assembly containing this
type is loaded.
At line:32 char:16
+         return & $origNewObject @psBoundParameters
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

This has only been tested with PowerShell 5.1 on WIndows 10 LTSC, build 1809

mithrandyr commented 3 years ago

@joalcorn -- can you confirm which version of Powershell you are using when using powershell.exe -- I know that PS v4 doesn't like to play nice with the '::new()' syntax. But PS v5.1 does.

Please run $PSVersionTable and post the results here. As an aside, this module has never intended to be compatible with versions of PowerShell prior to v5.

joalcorn commented 3 years ago

@mithrandyr I am using PowerShell 5.1.

Here is the output of $PSVersionTable as requested.

Name                           Value                                                                                                                                                                       
----                           -----                                                                                                                                                                       
PSVersion                      5.1.17763.2090                                                                                                                                                              
PSEdition                      Desktop                                                                                                                                                                     
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                     
BuildVersion                   10.0.17763.2090                                                                                                                                                             
CLRVersion                     4.0.30319.42000                                                                                                                                                             
WSManStackVersion              3.0                                                                                                                                                                         
PSRemotingProtocolVersion      2.3                                                                                                                                                                         
SerializationVersion           1.1.0.1    

The fix I submitted for this issue is to move from using the New-Object cmdlet to using the ::new() constructor off the type accelerator when instantiating the $Messages member of the ProviderBase class, which is what I committed in my pull request.