I’m facing an issue that I’m unable to resolve. I have a PowerShell script that uploads data to MySQL. The script works fine on my local machine, but when I deploy it to an Azure Function Application, I encounter some errors :
2024-08-08T12:39:13Z [Error] ERROR: Unable to find type [MySql.Data.MySqlClient.MySqlDbType].
And
Cannot find type [MySql.Data.MySqlClient.MySqlConnection]: verify that the assembly containing this type is loaded. InvalidOperation:
I think the MySql.Data.dll module is not being loaded correctly. I’ve been trying to find this module directly in the gallery, but I haven’t been able to locate it.
Here is my script:
# Input bindings are passed in via param block.
param($Timer)
# Get the current universal time in the default string format
#$currentUTCtime = (Get-Date).ToUniversalTime()
# The 'IsPastDue' property is 'true' when the current function invocation is later than scheduled.
if ($Timer.IsPastDue) {
Write-Host "PowerShell timer is running late!"
}
# Obtenir les valeurs des variables d'environnement
$subscriptionId = $env:FACT_SUB_ID
$tenantId = $env:FACT_TENANT_ID
$clientId = $env:FACT_API_CLIID
$clientSecret = $env:FACT_API_PWD
# Configuration de la connexion MySQL
$mysqlServer = $env:FACT_MYSQL_HOST
$mysqlDatabase = $env:FACT_MYSQL_DB
$mysqlUser = $env:FACT_MYSQL_USER
$mysqlPassword = $env:FACT_MYSQL_PWD
# Charger le module MySql.Data
#Add-Type -Path "MySql.Data.dll" # Remplacez par le chemin correct vers votre MySql.Data.dll
# Obtenir un jeton d'accès
$tokenRequestBody = @{
grant_type = "client_credentials"
client_id = $clientId
client_secret = $clientSecret
resource = "https://management.azure.com/"
}
$response = Invoke-RestMethod -Method Post -Uri "https://login.microsoftonline.com/$tenantId/oauth2/token" -ContentType "application/x-www-form-urlencoded" -Body $tokenRequestBody
$accessToken = $response.access_token
# Fonction pour appeler une API REST et obtenir des informations JSON
function Get-AzResource {
param (
[string]$url
)
return Invoke-RestMethod -Uri $url -Method Get -Headers @{ Authorization = "Bearer $accessToken" }
}
# Connexion à MySQL
$connectionString = "server=$mysqlServer;database=$mysqlDatabase;user=$mysqlUser;password=$mysqlPassword;"
$connection = New-Object MySql.Data.MySqlClient.MySqlConnection($connectionString)
$connection.Open()
# Obtenir les groupes de ressources
$resourceGroupsUrl = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups?api-version=2021-04-01"
$resourceGroups = Get-AzResource -url $resourceGroupsUrl
foreach ($rg in $resourceGroups.value) {
$currentRG = $rg.name
$storageAccountsUrl = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$currentRG/providers/Microsoft.Storage/storageAccounts?api-version=2021-04-01"
$storageAccounts = Get-AzResource -url $storageAccountsUrl
foreach ($sa in $storageAccounts.value) {
$storageAccount = $sa.name
$usedCapacityInGB = 0
$metricUrl = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$currentRG/providers/Microsoft.Storage/storageAccounts/$storageAccount/providers/microsoft.insights/metrics?api-version=2018-01-01&metricnames=UsedCapacity"
$metrics = Get-AzResource -url $metricUrl
$usedCapacity = $metrics.value[0].timeseries[0].data[0].average
if ($usedCapacity) {
$usedCapacityInGB = [math]::Round($usedCapacity / 1024 / 1024 / 1024, 2)
}
# Préparer et exécuter la requête SQL d'insertion
$query = @"
INSERT INTO SAUVEGARDES (client_id, storage_account, date, volume)
SELECT COALESCE(client_id, 1), @aliasName, CURDATE(), @volume
FROM CLIENTS
WHERE alias_sauvegardes = @aliasName
UNION
SELECT 1, @aliasName, CURDATE(), @volume
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM CLIENTS WHERE alias_sauvegardes = @aliasName)
"@
$command = $connection.CreateCommand()
$command.CommandText = $query
$command.Parameters.Add((New-Object MySql.Data.MySqlClient.MySqlParameter("@aliasName", [MySql.Data.MySqlClient.MySqlDbType]::VarChar, 255))).Value = $storageAccount
$command.Parameters.Add((New-Object MySql.Data.MySqlClient.MySqlParameter("@volume", [MySql.Data.MySqlClient.MySqlDbType]::Decimal, 10, 2))).Value = $usedCapacityInGB
$command.ExecuteNonQuery()
}
}
# Fermer la connexion à MySQL
$connection.Close()
I tried to use another module but it doesn’t work… I just want to automatically upload data without my computer (any service on Azure can be OK).
I’m a beginner in data …
Anyone can help me ? 🙂
Thank you so much in advance!
FlorianBB is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.