Advertisement
RJSN

Generate_SAS_token

Sep 28th, 2017
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # This PowerShell script is part of video https://www.youtube.com/watch?v=BI7F4-4zfEY
  2. #---------------------------------------------------------------------
  3. # Date              : 28-09-2017
  4. # Script name       : Generate_SAS_token.ps1
  5. # Description       : This script will generate a SAS token from the
  6. #                     Service Bus queue primary key.
  7. #                
  8. # Created by        : Basic Cloud
  9. # Extra module      :
  10. # Copyright         : ©2017 Basic Cloud, all rights reserved.
  11. # History           : Basic Cloud 20170928 Initial version
  12. #---------------------------------------------------------------------
  13.  
  14. [Reflection.Assembly]::LoadWithPartialName("System.Web")| out-null
  15.  
  16. # Enter the information about the Service Bus
  17. $URI="basiccloud.servicebus.windows.net/basiccloud/messages/head"
  18. $Access_Policy_Name="RootManageSharedAccessKey"
  19. $Access_Policy_Key="rd1sFC/Q1mySrpa8ygiJpyhd3wO5Q7yN5V0FycR0diU="
  20.  
  21. # Set the token expiration
  22. $Expires=([DateTimeOffset]::Now.ToUnixTimeSeconds())+300
  23.  
  24.  
  25. # Build the token
  26. $SignatureString=[System.Web.HttpUtility]::UrlEncode($URI)+ "`n" + [string]$Expires
  27.  
  28. $HMAC = New-Object System.Security.Cryptography.HMACSHA256
  29. $HMAC.key = [Text.Encoding]::ASCII.GetBytes($Access_Policy_Key)
  30.  
  31. $Signature = $HMAC.ComputeHash([Text.Encoding]::ASCII.GetBytes($SignatureString))
  32.  
  33. $Signature = [Convert]::ToBase64String($Signature)
  34. $SASToken = "SharedAccessSignature sr=" + [System.Web.HttpUtility]::UrlEncode($URI) + "&sig=" + [System.Web.HttpUtility]::UrlEncode($Signature) + "&se=" + $Expires + "&skn=" + $Access_Policy_Name
  35.  
  36. # Output the value
  37. $SASToken
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement