{"id":1495,"date":"2025-08-24T13:00:47","date_gmt":"2025-08-24T11:00:47","guid":{"rendered":"https:\/\/www.flip-design.de\/?p=1495"},"modified":"2025-08-24T13:00:47","modified_gmt":"2025-08-24T11:00:47","slug":"power-shell-script-to-send-a-simple-json-to-an-azure-event-hub","status":"publish","type":"post","link":"https:\/\/www.flip-design.de\/?p=1495","title":{"rendered":"Power Shell script to send a simple JSON to an Azure Event Hub"},"content":{"rendered":"\n<p>Until now, I&#8217;ve always used a C# program provided to me to send events to an Azure Event Hub. This actually became a bit too heavy-duty for me over time. So I looked for a PowerShell alternative, but couldn&#8217;t really find one at first, possibly because I wanted to learn it myself&#8230;<\/p>\n\n\n\n<p>The goal of the whole thing was to send events as JSON to an Azure Event Hub to process this data in Fabric Real Time.<\/p>\n\n\n\n<p>Here you can find the finished script, which is partly based on information from here, and partly I&#8217;ve supplemented it:<\/p>\n\n\n\n<p><a href=\"https:\/\/learn.microsoft.com\/en-us\/rest\/api\/eventhub\/generate-sas-token#code-try-8\">https:\/\/learn.microsoft.com\/en-us\/rest\/api\/eventhub\/generate-sas-token#code-try-8<\/a><\/p>\n\n\n\n<p>Here you can find the complete script, where the information for the Azure Event Hub still needs to be added:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Reflection.Assembly]::LoadWithPartialName(\"System.Web\")| out-null\r\n$URI=\"&lt;Event Hubs Namespace>.servicebus.windows.net\/&lt;Event Hub>\"\r\n$Access_Policy_Name=\"&lt;Shared access policies>\"\r\n$Access_Policy_Key=\"&lt;Key>\"\r\n#Token expires now+300\r\n$Expires=(&#91;DateTimeOffset]::Now.ToUnixTimeSeconds())+1800\r\n$SignatureString=&#91;System.Web.HttpUtility]::UrlEncode($URI)+ \"`n\" + &#91;string]$Expires\r\n$HMAC = New-Object System.Security.Cryptography.HMACSHA256\r\n$HMAC.key = &#91;Text.Encoding]::ASCII.GetBytes($Access_Policy_Key)\r\n$Signature = $HMAC.ComputeHash(&#91;Text.Encoding]::ASCII.GetBytes($SignatureString))\r\n$Signature = &#91;Convert]::ToBase64String($Signature)\r\n$SASToken = \"SharedAccessSignature sr=\" + &#91;System.Web.HttpUtility]::UrlEncode($URI) + \"&amp;sig=\" + &#91;System.Web.HttpUtility]::UrlEncode($Signature) + \"&amp;se=\" + $Expires + \"&amp;skn=\" + $Access_Policy_Name\r\n\r\n    $payload = @{\r\n        msg  = \"GDFJHZSHJK\"\r\n        time = (Get-Date).ToString(\"o\")\r\n    }\r\n    $json = $payload | ConvertTo-Json -Compress\r\n\r\n$endpoint = \"https:\/\/$URI\/messages?timeout=60&amp;api-version=2014-01\"\r\n$headers  = @{\r\n    \"Authorization\" = $SASToken\r\n    \"Content-Type\"  = \"application\/json; charset=utf-8\"\r\n}\r\n\r\nInvoke-RestMethod -Uri $endpoint -Method Post -Headers $headers -Body $json\r\n<\/code><\/pre>\n\n\n\n<p>When the script is running, you will see corresponding data like this coming in on the monitor:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2025\/08\/image.png\"><img decoding=\"async\" loading=\"lazy\" width=\"940\" height=\"385\" src=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2025\/08\/image.png\" alt=\"\" class=\"wp-image-1496\" srcset=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2025\/08\/image.png 940w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2025\/08\/image-300x123.png 300w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2025\/08\/image-768x315.png 768w\" sizes=\"(max-width: 940px) 100vw, 940px\" \/><\/a><\/figure>\n\n\n\n<p>I will explain how to proceed in Microsoft Fabric in the coming blog posts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Until now, I&#8217;ve always used a C# program provided to me to send events to an Azure Event Hub. This actually became a bit too heavy-duty for me over time. So I looked for a PowerShell alternative, but couldn&#8217;t really &hellip; <a href=\"https:\/\/www.flip-design.de\/?p=1495\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/posts\/1495"}],"collection":[{"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1495"}],"version-history":[{"count":1,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/posts\/1495\/revisions"}],"predecessor-version":[{"id":1497,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/posts\/1495\/revisions\/1497"}],"wp:attachment":[{"href":"https:\/\/www.flip-design.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}