I want to use a PowerShell script to imitate an IoT device that sends messages to and event hub in Azure. I’ve created the script below, but when I run it, it gives the error Send-AzEventHub’ is not recognized as the name of a cmdlet, function, script file, or operable program.
My PowerShell ISE is version 5 and PowerShell 7 is only a command line utility. And I can’t get PowerShell extension working in Visual Code.
So I tried the Cloud Shell in Azure portal, but I get the same message.
Anyone an idea how I can get a script going the simulated a IoT stream?
Install-Module Az.EventHub -Scope CurrentUser -Force -AllowClobber
Install-Module Az.Messaging.Eventhubs -Scope CurrentUser -Force -AllowClobber
Import-Module Az.EventHub -ErrorAction SilentlyContinue
Import-Module Az.Messaging.Eventhubs -ErrorAction SilentlyContinue
$EventHubConnectionString = “Endpoint=sb://ievxxxxbns.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=gMLcw9fGZvoxxxxxxxxxxxxxxxxxxxxxx1mb+AEhO6rvJM=”
$EventHubName = “ievxxxxhub”
$TruckIDs = @(“TRUCK-NL-001”, “TRUCK-BE-007”, “TRUCK-DE-042”, “TRUCK-FR-112”)
$SendIntervalSeconds = 5
$MinTemperature = -5.0 # Graden Celsius (bijv. voor gekoeld transport)
$MaxTemperature = 25.0 # Graden CelsiusWrite-Host “Starten van temperatuur simulatie voor vrachtwagens…”
Write-Host “Event Hub Namespace Connection String (gedeeltelijk): $($EventHubConnectionString.Split(‘;’)[0])”
Write-Host “Event Hub Naam: $EventHubName”
Write-Host “Druk op CTRL+C om te stoppen.”
Write-Host “—”try {
$i = 0
while ($true) {
$i++
Write-Host “`nCyclus $i - Data verzenden…”foreach ($TruckID in $TruckIDs) { # Simuleer temperatuur $CurrentTemperature = Get-Random -Minimum $MinTemperature -Maximum $MaxTemperature $CurrentTemperature = [Math]::Round($CurrentTemperature, 1) # Afronden op 1 decimaal # Simuleer een kleine variatie in locatie (optioneel) $Latitude = Get-Random -Minimum 50.0000 -Maximum 53.5000 $Longitude = Get-Random -Minimum 3.5000 -Maximum 7.2000 $Latitude = [Math]::Round($Latitude, 4) $Longitude = [Math]::Round($Longitude, 4) # Huidige tijdstip in UTC ISO 8601 formaat $Timestamp = (Get-Date).ToUniversalTime().ToString("o") # Creëer het data object (payload) $Payload = @{ TruckId = $TruckID Temperature = $CurrentTemperature Unit = "Celsius" Timestamp = $Timestamp Location = @{ Latitude = $Latitude Longitude = $Longitude } EventSource = "PowerShellSimulator" } # Converteer naar JSON $JsonPayload = $Payload | ConvertTo-Json -Compress try { # Verzend het bericht naar Event Hub # Send-AzEventHub verwacht een Namespace-level connection string en de Event Hub naam apart. Send-AzEventHub -ConnectionString $EventHubConnectionString -EventHubName $EventHubName -Message $JsonPayload Write-Host "[$Timestamp] Data voor $TruckID verzonden: Temp $($CurrentTemperature)°C. Payload: $JsonPayload" } catch { Write-Error "[$Timestamp] Fout bij verzenden data voor $TruckID naar Event Hub: $($_.Exception.Message)" # Optioneel: voeg hier logica toe om te stoppen of opnieuw te proberen na meerdere fouten } } Write-Host "---" Write-Host "Wachten voor $SendIntervalSeconds seconden tot de volgende cyclus..." Start-Sleep -Seconds $SendIntervalSeconds }
}
catch {
Write-Error “Er is een onverwachte fout opgetreden in de hoofdlus: $($_.Exception.Message)”
}
finally {
Write-Host “Script gestopt.”
}