Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This article shows you how to use Create and Manage Azure Notification Hubs using PowerShell. The following common automation tasks are shown in this article.
- Create a Notification Hub
- Set Credentials
If you also need to create a new service bus namespace for your notification hubs, see Manage Service Bus with PowerShell.
Managing Notifications Hubs is not supported directly by the cmdlets included with Azure PowerShell. The best approach from PowerShell is to reference the Microsoft.Azure.NotificationHubs.dll assembly. The assembly is distributed with the Azure Notification Hubs NuGet package.
- An Azure subscription. Azure is a subscription-based platform. For more information about obtaining a subscription, see Purchase Options, Member Offers, or trial subscription.
- A computer with Azure PowerShell. For instructions, see Install and configure Azure PowerShell.
- A general understanding of PowerShell scripts, NuGet packages, and the .NET Framework.
Managing Azure Notification Hubs is not yet included with the PowerShell cmdlets in Azure PowerShell. To provision notification hubs, you can use the .NET client provided in the Azure Notification Hubs NuGet package.
First, make sure your script can locate the Microsoft.Azure.NotificationHubs.dll assembly, which is installed as a NuGet package in a Visual Studio project. In order to be flexible, the script performs these steps:
- Determines the path at which it was invoked.
- Traverses the path until it finds a folder named
packages
. This folder is created when you install NuGet packages for Visual Studio projects. - Recursively searches the
packages
folder for an assembly namedMicrosoft.Azure.NotificationHubs.dll
. - References the assembly so that the types are available for later use.
Here's how these steps are implemented in a PowerShell script:
try
{
# WARNING: Make sure to reference the latest version of Microsoft.Azure.NotificationHubs.dll
Write-Output "Adding the [Microsoft.Azure.NotificationHubs.dll] assembly to the script..."
$scriptPath = Split-Path (Get-Variable MyInvocation -Scope 0).Value.MyCommand.Path
$packagesFolder = (Split-Path $scriptPath -Parent) + "\packages"
$assembly = Get-ChildItem $packagesFolder -Include "Microsoft.Azure.NotificationHubs.dll" -Recurse
Add-Type -Path $assembly.FullName
Write-Output "The [Microsoft.Azure.NotificationHubs.dll] assembly has been successfully added to the script."
}
catch [System.Exception]
{
Write-Error("Could not add the Microsoft.Azure.NotificationHubs.dll assembly to the script. Make sure you build the solution before running the provisioning script.")
}
To provision Notification Hubs, create an instance of the NamespaceManager class from the SDK.
You can use the Get-AzureSBAuthorizationRule cmdlet included with Azure PowerShell to retrieve an authorization rule that's used to provide a connection string. A reference to the NamespaceManager
instance is stored in the $NamespaceManager
variable. $NamespaceManager
is used to provision a notification hub.
$sbr = Get-AzureSBAuthorizationRule -Namespace $Namespace
# Create the NamespaceManager object to create the hub
Write-Output "Creating a NamespaceManager object for the [$Namespace] namespace..."
$NamespaceManager=[Microsoft.Azure.NotificationHubs.NamespaceManager]::CreateFromConnectionString($sbr.ConnectionString);
Write-Output "NamespaceManager object for the [$Namespace] namespace has been successfully created."
To provision a new notification hub, use the .NET API for Notification Hubs.
In this part of the script, you set up four local variables.
$Namespace
: Set this to the name of the namespace where you want to create a notification hub.$Path
: Set this path to the name of the new notification hub. For example, "MyHub".$WnsPackageSid
: Set this to the package SID for your Windows App from the Windows Dev Center.$WnsSecretkey
: Set this to the secret key for your Windows App from the Windows Dev Center.
These variables are used to connect to your namespace and create a new Notification Hub configured to handle Windows Notification Services (WNS) notifications with WNS credentials for a Windows App. For information on obtaining the package SID and secret key see, the Getting Started with Notification Hubs tutorial.
- The script snippet uses the
NamespaceManager
object to check to see if the Notification Hub identified by$Path
exists. - If it does not exist, the script creates
NotificationHubDescription
with WNS credentials and passes it to theNamespaceManager
classCreateNotificationHub
method.
$Namespace = "<Enter your namespace>"
$Path = "<Enter a name for your notification hub>"
$WnsPackageSid = "<your package sid>"
$WnsSecretkey = "<enter your secret key>"
$WnsCredential = New-Object -TypeName Microsoft.Azure.NotificationHubs.WnsCredential -ArgumentList $WnsPackageSid,$WnsSecretkey
# Query the namespace
$CurrentNamespace = Get-AzureSBNamespace -Name $Namespace
# Check if the namespace already exists
if ($CurrentNamespace)
{
Write-Output "The namespace [$Namespace] in the [$($CurrentNamespace.Region)] region was found."
# Create the NamespaceManager object used to create a new notification hub
$sbr = Get-AzureSBAuthorizationRule -Namespace $Namespace
Write-Output "Creating a NamespaceManager object for the [$Namespace] namespace..."
$NamespaceManager = [Microsoft.Azure.NotificationHubs.NamespaceManager]::CreateFromConnectionString($sbr.ConnectionString);
Write-Output "NamespaceManager object for the [$Namespace] namespace has been successfully created."
# Check to see if the Notification Hub already exists
if ($NamespaceManager.NotificationHubExists($Path))
{
Write-Output "The [$Path] notification hub already exists in the [$Namespace] namespace."
}
else
{
Write-Output "Creating the [$Path] notification hub in the [$Namespace] namespace."
$NHDescription = New-Object -TypeName Microsoft.Azure.NotificationHubs.NotificationHubDescription -ArgumentList $Path;
$NHDescription.WnsCredential = $WnsCredential;
$NamespaceManager.CreateNotificationHub($NHDescription);
Write-Output "The [$Path] notification hub was created in the [$Namespace] namespace."
}
}
else
{
Write-Host "The [$Namespace] namespace does not exist."
}
- Manage Service Bus with PowerShell
- How to create Service Bus queues, topics and subscriptions using a PowerShell script
- How to create a Service Bus Namespace and an Event Hub using a PowerShell script
Some ready-made scripts are also available for download: