none
How to send data to EventHub using Powershell RRS feed

  • Question

  • Hi Expert,

    I struggled to send data to an EventHub using Powershell. I hope someone can help me.

    First: documentation here from MS but i think I really did not understand ;(

    Second: below the code I used

    #################################
    # API method
    $method = "POST"
    
    # API header
    $headers = @{
                "Authorization"="SharedAccessSignature sr={my namespace here}.servicebus.windows.net&sig={my key here}&se=1444603552&skn=RootManageSharedAccessKey";
                "Content-Type"="application/atom+xml;type=entry;charset=utf-8";
                "Host"="{my name space here}.servicebus.windows.net";
                "ContentLength" = "42"
                }
                
    # generate the API URI
    $URI = "https://{my namespace here}.servicebus.windows.net/{my event hub name here}/messages?timeout=60&api-version=2014-01 HTTP/1.1"
    
    
    
    # create Request Body - What i missed here ???
    # $body = { "DeviceId":"dev-01", "Temperature":"37.0" }
    
    
    # execute the Azure REST API
    Invoke-RestMethod -Uri $URI -Method $method -Headers $headers -Body $body 

    On computer i added my azure account and already import azure published settings.

    Problem: i always get error 401 Remote server  not authorized.

    Did you experience such error ? Maybe you will catch quickly what i missed or typed wrong.

    Regards,

    Terry

    Sunday, October 11, 2015 2:48 PM

Answers

  • Hi Terry, It's not particularly elegant, but this should work:

    # Load the System.Web assembly to enable UrlEncode
    [Reflection.Assembly]::LoadFile( `
      'C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\System.Web.dll')`
      | out-null 
    
    $method = "POST"
    $URI = "https://YOURNS.servicebus.windows.net/YOURHUB/messages"
    $encodedURI = [System.Web.HttpUtility]::UrlEncode($URI)
    $keyname = "sendpolicy"
    $key = "1MsXmv...YOURKEY"
    $startDate = [datetime]”01/01/1970 00:00”
    $hour = New-TimeSpan -Hours 1
    
    # Calculate expiry value one hour ahead
    $sinceEpoch = NEW-TIMESPAN –Start $startDate –End ((Get-Date) + $hour)
    $expiry = [Math]::Floor([decimal]($sinceEpoch.TotalSeconds + 3600))
    
    # Create the signature
    $stringToSign = $encodedURI + "`n" + $expiry
    $hmacsha = New-Object System.Security.Cryptography.HMACSHA256
    $hmacsha.key = [Text.Encoding]::ASCII.GetBytes($key)
    $signature = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($stringToSign))
    $signature = [System.Web.HttpUtility]::UrlEncode([Convert]::ToBase64String($signature))
    
    # API headers
    $headers = @{
                "Authorization"="SharedAccessSignature sr=" + $encodedURI + "&sig=" + $signature + "&se=" + $expiry + "&skn=" + $keyname;
                "Content-Type"="application/atom+xml;type=entry;charset=utf-8";
                "Content-Length" = "43"
                }
    
    
    # create Request Body
    $body = "{'DeviceId':'dev-01', 'Temperature':'37.0'}"
    
    # execute the Azure REST API
    Invoke-RestMethod -Uri $URI -Method $method -Headers $headers -Body $body


    Dominic Betts

    • Marked as answer by terry Caps Tuesday, October 20, 2015 7:15 PM
    Tuesday, October 20, 2015 9:23 AM

All replies

  • Hi,

    Thank you for reaching out to us. I am currently researching to gather more information with regards to your request. I shall revert back to you with an update at the earliest. Sincerely appreciate your patience.

    Regards,

    Asha

    Monday, October 12, 2015 10:09 AM
    Moderator
  • If you're directly using SAS key in the auth string you need to know that it won't work. Key needs to be encyripted first. Look at below reading.

    http://developers.de/blogs/damir_dobric/archive/2013/10/17/how-to-create-shared-access-signature-for-service-bus.aspx

    Monday, October 12, 2015 5:18 PM
  • Hi Terry, It's not particularly elegant, but this should work:

    # Load the System.Web assembly to enable UrlEncode
    [Reflection.Assembly]::LoadFile( `
      'C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\System.Web.dll')`
      | out-null 
    
    $method = "POST"
    $URI = "https://YOURNS.servicebus.windows.net/YOURHUB/messages"
    $encodedURI = [System.Web.HttpUtility]::UrlEncode($URI)
    $keyname = "sendpolicy"
    $key = "1MsXmv...YOURKEY"
    $startDate = [datetime]”01/01/1970 00:00”
    $hour = New-TimeSpan -Hours 1
    
    # Calculate expiry value one hour ahead
    $sinceEpoch = NEW-TIMESPAN –Start $startDate –End ((Get-Date) + $hour)
    $expiry = [Math]::Floor([decimal]($sinceEpoch.TotalSeconds + 3600))
    
    # Create the signature
    $stringToSign = $encodedURI + "`n" + $expiry
    $hmacsha = New-Object System.Security.Cryptography.HMACSHA256
    $hmacsha.key = [Text.Encoding]::ASCII.GetBytes($key)
    $signature = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($stringToSign))
    $signature = [System.Web.HttpUtility]::UrlEncode([Convert]::ToBase64String($signature))
    
    # API headers
    $headers = @{
                "Authorization"="SharedAccessSignature sr=" + $encodedURI + "&sig=" + $signature + "&se=" + $expiry + "&skn=" + $keyname;
                "Content-Type"="application/atom+xml;type=entry;charset=utf-8";
                "Content-Length" = "43"
                }
    
    
    # create Request Body
    $body = "{'DeviceId':'dev-01', 'Temperature':'37.0'}"
    
    # execute the Azure REST API
    Invoke-RestMethod -Uri $URI -Method $method -Headers $headers -Body $body


    Dominic Betts

    • Marked as answer by terry Caps Tuesday, October 20, 2015 7:15 PM
    Tuesday, October 20, 2015 9:23 AM
  • Hi Dominic,

    Thanks for your reply. How to say .... it is simply AWESOME !

    I confirm your solution works like a charm. You make my day!

    Regards,

    Terrt

    Tuesday, October 20, 2015 7:17 PM