Advertisement
RJSN

Remove_messages_using_PowerShell

Sep 28th, 2017
995
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       : Remove message using_PowerShell.ps1
  5. # Description       : This script will remove messages from a
  6. #                     Service Bus queue.
  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. <#
  15. # only run this code if you do not have a SAStoken.
  16. [Reflection.Assembly]::LoadWithPartialName("System.Web")| out-null
  17.  
  18. # Enter the information about the Service Bus
  19. $URI="basiccloud.servicebus.windows.net/basiccloud/messages/head"
  20. $Access_Policy_Name="basiccloud"
  21. $Access_Policy_Key="fVGxhG2j5tJIGHstH6hoX7rVKLoOAMHaycnWJnCE/r8="
  22.  
  23. # Set the token expiration
  24. $Expires=([DateTimeOffset]::Now.ToUnixTimeSeconds())+300
  25.  
  26.  
  27. # Build the token
  28. $SignatureString=[System.Web.HttpUtility]::UrlEncode($URI)+ "`n" + [string]$Expires
  29.  
  30. $HMAC = New-Object System.Security.Cryptography.HMACSHA256
  31. $HMAC.key = [Text.Encoding]::ASCII.GetBytes($Access_Policy_Key)
  32.  
  33. $Signature = $HMAC.ComputeHash([Text.Encoding]::ASCII.GetBytes($SignatureString))
  34.  
  35. $Signature = [Convert]::ToBase64String($Signature)
  36. $SASToken = "SharedAccessSignature sr=" + [System.Web.HttpUtility]::UrlEncode($URI) + "&sig=" + [System.Web.HttpUtility]::UrlEncode($Signature) + "&se=" + $Expires + "&skn=" + $Access_Policy_Name
  37.  
  38. # Output the value
  39. $SASToken
  40. #>
  41.  
  42. # Remove messages from the Service Bus queue
  43.  
  44. $header = @{"Authorization"="$SASToken"}
  45.  
  46. 0..5000 | ForEach-Object `
  47. -Begin { "Removing"; $counter = 0 } `
  48. -Process {
  49.             Invoke-RestMethod -Uri https://basiccloud.servicebus.windows.net/basiccloud/messages/head -Method Delete -Headers $header
  50.          } `
  51. -End { "Finished: $counter" }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement