{"id":668,"date":"2023-06-09T16:29:00","date_gmt":"2023-06-09T16:29:00","guid":{"rendered":"http:\/\/draith.com\/?p=668"},"modified":"2024-02-17T01:36:34","modified_gmt":"2024-02-17T01:36:34","slug":"quick-code-send-events-to-an-event-hub-with-powershell","status":"publish","type":"post","link":"https:\/\/draith.com\/?p=668","title":{"rendered":"Quick Code &#8211; Send Events to an Event Hub with PowerShell"},"content":{"rendered":"\n<p>Here is another quick one &#8211; let&#8217;s send events to an event hub with PowerShell!<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function New-SasToken {\n    param(\n        [string]$ResourceUri,\n        [string]$Key,\n        [string]$PolicyName,\n        [timespan]$TokenTimeToLive\n    )\n\n    $Expires = [DateTimeOffset]::Now.Add($TokenTimeToLive).ToUnixTimeSeconds()\n    $StringToSign = [System.Web.HttpUtility]::UrlEncode($ResourceUri) + \"`n\" + $Expires\n    $HMACSHA256 = New-Object System.Security.Cryptography.HMACSHA256\n    $HMACSHA256.Key = [Text.Encoding]::UTF8.GetBytes($Key)\n    $Signature = $HMACSHA256.ComputeHash([Text.Encoding]::UTF8.GetBytes($StringToSign))\n    $Signature = [Convert]::ToBase64String($Signature)\n    $Token = \"SharedAccessSignature sr=\" + [System.Web.HttpUtility]::UrlEncode($ResourceUri) + \"&sig=\" + [System.Web.HttpUtility]::UrlEncode($Signature) + \"&se=\" + $Expires + \"&skn=\" + $PolicyName\n    return $Token\n}\n\n# Event Hub parameters\n$namespace = \"yourNamespace\"\n$eventHubName = \"yourEventHubName\"\n$sharedAccessKeyName = \"yourSharedAccessKeyName\"\n$sharedAccessKey = \"yourSharedAccessKey\"\n$endpoint = \"https:\/\/$namespace.servicebus.windows.net\/$eventHubName\/messages\"\n$tokenTimeToLive = New-TimeSpan -Minutes 60\n\n# Generate SAS token\n$sasToken = New-SasToken -ResourceUri $endpoint -Key $sharedAccessKey -PolicyName $sharedAccessKeyName -TokenTimeToLive $tokenTimeToLive\n\n# Event data\n$body = @\"\n{\n    \"Data\": \"Sample Event Data\"\n}\n\"@\n\n# Send the event\n$headers = @{\n    \"Authorization\" = $sasToken\n    \"Content-Type\" = \"application\/json\"\n}\n\ntry {\n    $response = Invoke-RestMethod -Uri $endpoint -Method Post -Body $body -Headers $headers\n    Write-Output \"Event sent successfully\"\n}\ncatch {\n    Write-Error \"Failed to send event: $_\"\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is another quick one &#8211; let&#8217;s send events to an event hub with PowerShell! function New-SasToken { param( [string]$ResourceUri, [string]$Key, [string]$PolicyName, [timespan]$TokenTimeToLive ) $Expires = [DateTimeOffset]::Now.Add($TokenTimeToLive).ToUnixTimeSeconds() $StringToSign = [System.Web.HttpUtility]::UrlEncode($ResourceUri) + &#8220;`n&#8221; + $Expires $HMACSHA256 = New-Object System.Security.Cryptography.HMACSHA256 $HMACSHA256.Key = [Text.Encoding]::UTF8.GetBytes($Key) $Signature = $HMACSHA256.ComputeHash([Text.Encoding]::UTF8.GetBytes($StringToSign)) $Signature = [Convert]::ToBase64String($Signature) $Token = &#8220;SharedAccessSignature sr=&#8221; + [System.Web.HttpUtility]::UrlEncode($ResourceUri) + &#8220;&#038;sig=&#8221; &hellip; <a href=\"https:\/\/draith.com\/?p=668\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Quick Code &#8211; Send Events to an Event Hub with PowerShell&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4,16],"class_list":["post-668","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-azure","tag-powershell"],"_links":{"self":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=668"}],"version-history":[{"count":1,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/668\/revisions"}],"predecessor-version":[{"id":669,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/668\/revisions\/669"}],"wp:attachment":[{"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}