Forked from rberrelleza/UseServiceBusFromPowershell.ps1
Created
February 12, 2018 17:45
-
-
Save mnjstwins/e1f75cd91c12a6dd2aa9ee7d3f5bcc15 to your computer and use it in GitHub Desktop.
Send and receive messages to a Service Bus queue via PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import-Module "PATH_TO_Microsoft.ServiceBus.dll" | |
#Create the required credentials | |
$tokenProvider = [Microsoft.ServiceBus.TokenProvider]::CreateSharedSecretTokenProvider("owner", "YOUR_ISSUERSECRET") | |
$namespaceUri = [Microsoft.ServiceBus.ServiceBusEnvironment]::CreateServiceUri("sb", "YOUR_NAMESPACE", ""); | |
$namespaceManager = New-Object Microsoft.ServiceBus.NamespaceManager $namespaceUri,$tokenProvider | |
#Create a queue | |
$queue = $namespaceManager.CreateQueue("MyPowershellQueue"); | |
#Create a message factory | |
$factory = [Microsoft.ServiceBus.Messaging.MessagingFactory]::Create($namespaceUri, $tokenProvider) | |
#Create a sender (with ReceiveAndDelete mode) and send a few messages | |
$sender = $factory.CreateMessageSender($queue.Path) | |
#Send a few messages | |
1..10 | ForEach-Object { $sender.Send("Message from Powershell " + [Guid]::NewGuid()) } | |
#Create a message receiver (with ReceiveAndDelete mode) and receive the messages | |
$receiver = $factory.CreateMessageReceiver($queue.Path, [Microsoft.ServiceBus.Messaging.ReceiveMode]::ReceiveAndDelete) | |
#Receive the messages | |
1..10 | ForEach-Object { Write-Output "Received " + $receiver.Receive([TimeSpan]::FromSeconds(10))} | |
#Close the factory | |
$factory.Close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment