Created
March 21, 2023 15:40
-
-
Save RylandDeGregory/67fa1ca9d75e15a967a4ffa6130bc45c to your computer and use it in GitHub Desktop.
Add custom event to Azure Event Hub using PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Reflection.Assembly]::LoadWithPartialName('System.Web') | Out-Null | |
$HMAC = New-Object System.Security.Cryptography.HMACSHA256 | |
$EventHubsNamespace = '' | |
$EventHubName = '' | |
$EventHubEndpoint = "$EventHubsNamespace.servicebus.windows.net/$EventHubName" | |
$AccessPolicyName = 'RootManageSharedAccessKey' | |
$AccessPolicyKey = '' | |
$SASExpirationTime = ([DateTimeOffset]::Now.ToUnixTimeSeconds()) + 3000 | |
$SignatureString = [System.Web.HttpUtility]::UrlEncode($EventHubEndpoint) + "`n" + [string]$SASExpirationTime | |
$HMAC.Key = [Text.Encoding]::ASCII.GetBytes($AccessPolicyKey) | |
$Signature = [Convert]::ToBase64String($HMAC.ComputeHash([Text.Encoding]::ASCII.GetBytes($SignatureString))) | |
$SASToken = 'SharedAccessSignature sr=' + [System.Web.HttpUtility]::UrlEncode($EventHubEndpoint) + '&sig=' + [System.Web.HttpUtility]::UrlEncode($Signature) + '&se=' + $SASExpirationTime + '&skn=' + $AccessPolicyName | |
$Headers = @{ | |
'Authorization' = $SASToken; | |
'Content-Type' = 'application/atom+xml;type=entry;charset=utf-8'; | |
} | |
$RestUri = "https://$EventHubEndpoint/messages" | |
# Body contains a sample JSON object | |
$Body = @{ | |
DeviceId = 'dev-01' | |
Temperature = 37 | |
Elements = @( | |
'hot' | |
'cold' | |
) | |
} | ConvertTo-Json | |
Invoke-RestMethod -Uri $RestUri -Method Post -Headers $Headers -Body $Body |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment