Deploy and manage notification hubs using PowerShell

Overview

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 Microsoft Azure Notification Hubs NuGet package.

Prerequisites

Including a reference to the .NET assembly for Service Bus

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 Microsoft 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:

  1. Determines the path at which it was invoked.
  2. Traverses the path until it finds a folder named packages. This folder is created when you install NuGet packages for Visual Studio projects.
  3. Recursively searches the packages folder for an assembly named Microsoft.Azure.NotificationHubs.dll.
  4. 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.")
}

Create the NamespaceManager class

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."

Provisioning a new Notification Hub

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.

  1. $Namespace: Set this to the name of the namespace where you want to create a notification hub.
  2. $Path: Set this path to the name of the new notification hub. For example, "MyHub".
  3. $WnsPackageSid: Set this to the package SID for your Windows App from the Windows Dev Center.
  4. $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 the NamespaceManager class CreateNotificationHub 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."
}

Additional Resources

Some ready-made scripts are also available for download: