Hi Adam,
Well, I got this to work. I will document what I do to make this work so others might benefit from this.
The key thing to take away from this is:
- PSU v3 is based on Powershell 7.2 and that is using .Net v6
- PSU v4 is/will be based on Powershell 7.3 and that is using .Net v7
If you look inside the container and start Powershell you will see that this Powershell 7.3
Any experiment in there is not valid for what PSU is using, you will be testing PS 7.3 with .Net v7
If you start a Terminal inside PSU you will see it is Powershell v7.2 and that is the correct version to base your installation needs on.
So knowing we have PSU v3 and using .Net v6, we need to get the .Net Core version of the MySql Data DLL which is compatible with .Net v6
Here is a location to get that .Net Core version of the DLL:
NuGet Gallery | MySql.Data 8.2.0
https://www.nuget.org/api/v2/package/MySql.Data/8.0.33
Note: the downloaded file ‘mysql.data.8.0.33.nupkg’ can be renamed to ‘mysql.data.8.0.33.zip’ so you can unpack it
Now pay attention to the folder structure inside the ZIP/package, you will see many variations for this Data DLL and you will need to get the ‘MySql.Data.dll’ from the folder ‘net6.0’ inside the ZIP/Package.
Ok, now you have the right DLL. I will assume that the need is to query MySql so we will use the module ‘MySql’ from Github:
Download the package and put the ‘MySql’ folder in your Modules folder on your running instance of PSU. In my case it is a Docker container so I copied the ‘MySql’ folder to “/root/.PowerShellUniversal/Repository/Modules”. Copy the “MySql.Data.dll” from the ZIP/Package to this same folder as well.
Now we need to change the psd1 and the psm1 files from that module package ‘MySql’
Open the files “MySql.psd1” and “MySql.psm1” in an editor and change these like so.
MySql.psd1
Change this line:
RootModule = 'MySql.psm1'
To this:
RootModule = "$PSScriptRoot/MySql.psm1"
MySql.psm1
Change this line:
$null = [System.Reflection.Assembly]::LoadWithPartialName('MySql.Data')
To this:
[void][system.reflection.Assembly]::LoadFrom("$PSScriptRoot/MySql.Data.dll")
If all worked then restart PSU so that this module is picked up automatically by PSU.
Check once PSU is running again in the modules section of PSU:
Platform → Modules
Search Local for “MySql”
You should see the module listed by PSU:
Now to test this out you could create a Script (Automation → Scripts) and use this as content:
Import-Module -Name MySql
[MySql.Data.MySqlClient.MySqlCommand] | gm
If you see a message like:
Cannot find type [MySql.Data.MySqlClient.MySqlConnection]
or:
[error] Could not load file or assembly 'System.Runtime, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
Then you still have an issue to work out. The first one indicates that the DLL is not loaded, the second message indicates you are using the wrong DLL version (you must use the v6 version of the Data DLL).
If you get some output listing the methods on the Type ‘MySql.Data.MySqlClient.MySqlCommand’ then you are probably in a good state.
Test further with this type of test as content for the Script (replacing the previous test):
Import-Module -Name MySql
$mysqlServer = '<server_address>'
$mysqlId = '<mysql_username>'
$mysqlPassword = '<mysql_password>'
$mysqlPort = 3306
$mysqlDatabase = '<mysql_database>'
$Sql = 'SELECT version()'
[securestring]$secStringPassword = ConvertTo-SecureString $mysqlPassword -AsPlainText -Force
[pscredential]$Cred = New-Object System.Management.Automation.PSCredential ($mysqlId, $secStringPassword)
$Conn = Connect-MySqlServer -ComputerName $mysqlServer -Credential $Cred -Database $mysqlDatabase -Port $mysqlPort
Invoke-MySqlQuery -Connection $Conn -Query $Sql
The results should look something like this:
[information] version()
[information] ---------
[information] 8.0.33
Good luck!