Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Azure PowerShell Function App to send message to Azure IoT Device
# POST method: $req
$requestBody = Get-Content $req -Raw | ConvertFrom-Json
# Incoming Variables
$RGName = $requestBody.RGName
$IoTHubName = $requestBody.IoTHubName
$deviceID = $requestBody.deviceID
$IoTKeyName = $requestBody.IoTKeyName
$Message = $requestBody.msg
# MSI Variables via Function Application Settings Variables
# Endpoint and Password
$endpoint = $env:MSI_ENDPOINT
$secret = $env:MSI_SECRET
# Vault URI to get AuthN Token
$vaultTokenURI = 'https://vault.azure.net&api-version=2017-09-01'
# Our Key Vault Credential that we want to retreive URI obtained from the credential in the Keyvault
# make sure to add the following api version to the end /?api-version=2015-06-01'
$vaultSecretURI = 'https://mykeyvault.vault.azure.net/secrets/myKey/576971bba5c249f4abba7c38e69742fb/?api-version=2015-06-01'
# Create AuthN Header with our Function App Secret
$header = @{'Secret' = $secret}
# Get Key Vault AuthN Token
$authenticationResult = Invoke-RestMethod -Method Get -Headers $header -Uri ($endpoint +'?resource=' +$vaultTokenURI)
# Use Key Vault AuthN Token to create Request Header
$requestHeader = @{ Authorization = "Bearer $($authenticationResult.access_token)" }
# Call the Vault and Retrieve Creds
$creds = Invoke-RestMethod -Method GET -Uri $vaultSecretURI -ContentType 'application/json' -Headers $requestHeader
# Load PS Modules
import-module "D:\home\site\wwwroot\myFunctionApp\bin\AzureRM.profile\4.2.0\AzureRM.Profile.psm1" -Global
import-module "D:\home\site\wwwroot\myFunctionApp\bin\AzureRM\5.5.0\AzureRM.psm1" -Global
import-module "D:\home\site\wwwroot\myFunctionApp\bin\AzureIoT\1.0.0.5\AzureIoT.psm1" -Global
import-module "D:\home\site\wwwroot\myFunctionApp\bin\AzureRM.IotHub\3.1.0\AzureRM.IotHub.psm1" -Global
$username = 'user@mytenant.onmicrosoft.com'
$password = ConvertTo-SecureString $creds.value –asplaintext –force
$credentials = New-Object System.Management.Automation.PSCredential $Username,$password
$azaccount = Login-AzureRmAccount -Credential $credentials -TenantId '833bb743-1234-5678-abcd-123456789' -SubscriptionId 'abc123ef-1234-4ae7-ac64-1234567890'
# Get IoT Hub and Key
$IoTHub = Get-AzureRmIotHub -Name $IoTHubName -ResourceGroupName $RGName
$IoTHubKey = Get-AzureRmIotHubKey -ResourceGroupName $iothub.Resourcegroup -Name $IoTHubName -KeyName $IoTKeyName
# IoT ConnectionString
$IoTConnectionString = "HostName=$($IoTHubName).azure-devices.net;SharedAccessKeyName=$($IoTKeyName);SharedAccessKey=$($IoTHubKey.PrimaryKey)"
# Build Hub Device URI
$IOTHubDeviceURI= "$($IoTHubName).azure-devices.net/devices/$($deviceID)"
$deviceParams = @{
iotConnString = $IoTConnectionString
deviceId = $deviceID
}
# Device Keys
$deviceKeys = Get-IoTDeviceKey @deviceParams
# Get Device
$device = Get-IoTDeviceClient -iotHubUri $IOTHubDeviceURI -deviceId $deviceID -deviceKey $deviceKeys.DevicePrimaryKey
# Azure IoT Cloud Client
$CloudClientParams = @{
iotConnString = $IoTConnectionString
}
$cloudClient = Get-IoTCloudClient @CloudClientParams
# Send message from Cloud
$cloudMessageParams = @{
deviceId = $deviceID
messageString = $Message
cloudClient = $cloudClient
}
Send-IoTCloudMessage @cloudMessageParams
Out-File -Encoding Ascii -FilePath $res -inputObject "Message: $msg sent to DeviceID: $deviceID"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment