Skip to content

Instantly share code, notes, and snippets.

@Wind010
Created February 3, 2023 01:46
Show Gist options
  • Select an option

  • Save Wind010/548f4b25e9bf5b08125063599336a769 to your computer and use it in GitHub Desktop.

Select an option

Save Wind010/548f4b25e9bf5b08125063599336a769 to your computer and use it in GitHub Desktop.
A module that contains logic to send/publish event messages to specified Azure Event Hub.
function Get-SASToken
{
<#
.DESCRIPTION
Compute a valid SAS-token for accessing an Azure Event Hub / Service Bus
.PARAMETER EventHubNamespace
The Azure Event Hub Namespace
.PARAMETER EventHubName
Name of the of the Azure Event Hub
.PARAMETER AccessPolicyName
Name of the access policy
.PARAMETER AccessPolicyKey
Key for the access policy
.PARAMETER TokenTimeOut
Timeout in seconds for the SAS-token (default 1800 seconds)
.EXAMPLE
Get-SASToken -EventHubNamespace 'YourEventHubNamespace' -EventHubName 'YourEventHubName' -AccessPolicyName "YourSendPolicyName" -AccessPolicyKey "RVZFTlRfSFVCX0FDQ0VTU19QT0xJQ1lfS0VZIQ=="
.NOTES
Author: Jeff Tong
Reference: https://learn.microsoft.com/en-us/rest/api/eventhub/generate-sas-token
#>
PARAM(
[Parameter(Mandatory=$True)]
[string] $EventHubNamespace,
[Parameter(Mandatory=$True)]
[string] $EventHubName,
[Parameter(Mandatory=$True)]
[string] $AccessPolicyName,
[Parameter(Mandatory=$True)]
[string] $AccessPolicyKey,
[int] $TokenTimeOut=1800
)
[Reflection.Assembly]::LoadWithPartialName("System.Web")| out-null
$URI="$eventHubNamespace.servicebus.windows.net/$eventHubName"
$Expires=([DateTimeOffset]::Now.ToUnixTimeSeconds()) + $TokenTimeOut
$SignatureString=[System.Web.HttpUtility]::UrlEncode($URI)+ "`n" + [string]$Expires
$HMAC = New-Object System.Security.Cryptography.HMACSHA256
$HMAC.key = [Text.Encoding]::ASCII.GetBytes($AccessPolicyKey)
$Signature = $HMAC.ComputeHash([Text.Encoding]::ASCII.GetBytes($SignatureString))
$Signature = [Convert]::ToBase64String($Signature)
$SASToken = "SharedAccessSignature sr=" + [System.Web.HttpUtility]::UrlEncode($URI) + "&sig=" + [System.Web.HttpUtility]::UrlEncode($Signature) + "&se=" + $Expires + "&skn=" + $AccessPolicyName
return $SASToken
}
function Send-EventHubMessage
{
<#
.DESCRIPTION
Send a message to an Azure Event Hub
.PARAMETER EventHubNamespace
The Azure Event Hub Namespace
.PARAMETER EventHubName
Name of the of the Azure Event Hub
.PARAMETER SASToken
A valid SAS authorization token (see Get-SASToken)
.PARAMETER Message
Message send to the specified eventhub Event Hub in json format. E.g.: { "key":"123456789", "value":"123.00"}
.PARAMETER TimeOut
The Timeout for this post with default of 30 seconds.
.PARAMETER PublisherId
Send the message to specific publisher by ID. Default is null.
.PARAMETER APIVersion
API-version string. Default is null.
.EXAMPLE
Send-EventHubMessage -EventHubNamespace 'yourEventHubNamespace' -EventHubName 'yourEventHubName' -SASToken $SASToken -Message ' { "key":"123456789", "value":"123.00"}'
.NOTES
Author: Jeff Tong
Reference: https://learn.microsoft.com/en-us/rest/api/eventhub/send-event
#>
PARAM(
[Parameter(Mandatory=$True)]
[string] $EventHubNamespace,
[Parameter(Mandatory=$True)]
[string] $EventHubName,
[Parameter(Mandatory=$True)]
[string] $SASToken,
[Parameter(Mandatory=$True)]
[string] $Message,
[Parameter(Mandatory=$False)]
[int] $TimeOut=30,
[Parameter(Mandatory=$False)]
[string] $PublisherId=$null,
[Parameter(Mandatory=$False)]
[string] $APIVersion=$null
)
$headers = @{
"Authorization"=$SASToken;
"Content-Type"="application/atom+xml;type=entry;charset=utf-8";
}
$Uri = "https://$($EventHubNamespace).servicebus.windows.net/$($EventHubName)"
if ([string]::IsNullOrWhitespace($PublisherId))
{
$Uri = $Uri + "/messages?timeout=$($TimeOut)"
}
else
{
$Uri = $Uri + "/publishers/$(PublisherId)/messages?timeout=$($TimeOut)"
}
if (-not [string]::IsNullOrWhitespace($APIVersion))
{
$Uri = "$($Uri)&api-version=$($APIVersion)"
}
# Should see a 202 - Created
return Invoke-WebRequest -Method POST -Uri $Uri -Header $headers -Body $Message
}
@Wind010
Copy link
Copy Markdown
Author

Wind010 commented Feb 3, 2023

If 404 - Unauthorized is received when sending the event message, double check the access key and access key policy name. Also ensure that your IP Address is added/allowed to the Event Hub Namespace resource if Firewall is enabled (Networking -> Firewall).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment