Quantcast
Channel: Service Bus forum
Viewing all articles
Browse latest Browse all 1916

Automation of IoT Hub using PowerShell

$
0
0

Here’s the code:

 How do I create the SAS token using PowerShell?

# 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="GET"

$URI='https://nameofiot.azure-devices.net/devices?top=10&api-version=“2015-08-15-preview"'

 

 

 

$encodedURI=[System.Web.HttpUtility]::UrlEncode($URI)

$policyname="iothubowner"

$primarykey="EA…."

$startDate=[datetime]”01/01/1970 00:00”

$hour=New-TimeSpan-Hours1

 

# 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($primarykey)

$signature=$hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($stringToSign))

$signature=[System.Web.HttpUtility]::UrlEncode([Convert]::ToBase64String($signature))

 

# API headers

$headers= @{

           "Authorization"="SharedAccessSignature sig="+"$signature"+"&se="+$expiry+"&sr="+$encodedURI;

           "Content-Type"="application/json";

            }

 

# execute the Azure REST API

Invoke-RestMethod-Uri$URI -Method$method -Headers$headers

 

 

It would be great if someone could help me out with this!

Viewing all articles
Browse latest Browse all 1916

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>