Skip to content

Instantly share code, notes, and snippets.

@kstrauss
Created July 12, 2016 12:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kstrauss/96948f08380719c40b8cd1e6f1bc4fc0 to your computer and use it in GitHub Desktop.
Save kstrauss/96948f08380719c40b8cd1e6f1bc4fc0 to your computer and use it in GitHub Desktop.
Publish to an azure event hub from powershell
$ehName = "powershellposttest" # hub name
$ehNameSpace = "RFGateLgEUS" # namespace
$keyname = "senderPolicy" # name of the policy
$key = "yourKEY"
# Load the System.Web assembly to enable UrlEncode
[Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null
$URI = "{0}.servicebus.windows.net/{1}" -f @($ehNameSpace,$ehName)
$encodedURI = [System.Web.HttpUtility]::UrlEncode($URI)
# Calculate expiry value one hour ahead
$expiry = [string](([DateTimeOffset]::Now.ToUnixTimeSeconds())+3600)
# Create the signature
$stringToSign = [System.Web.HttpUtility]::UrlEncode($URI) + "`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))
# create Request Body
$body = "{'DeviceId':'dev-01', 'Temperature':'37.0'}"
# API headers
#
$headers = @{
"Authorization"="SharedAccessSignature sr=" + $encodedURI + "&sig=" + $signature + "&se=" + $expiry + "&skn=" + $keyname;
"Content-Type"="application/atom+xml;type=entry;charset=utf-8"; # must be this
"Content-Length" = ("{0}" -f ($body.Length))
}
# execute the Azure REST API
$method = "POST"
$dest = 'https://' +$URI +'/messages?timeout=60&api-version=2014-01'
<# used for turning off certificate checking in case you are using fiddler or other man in the middle#>
<#
add-type -TypeDefinition @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
#>
Invoke-RestMethod -Uri $dest -Method $method -Headers $headers -Body $body -Verbose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment