Distribuire e gestire Hub di notifica tramite PowerShell

Panoramica

In questo articolo viene illustrato come utilizzare Creazione e gestione di hub di notifica di Azure tramite PowerShell. Le seguenti attività comuni di automazione sono illustrate in questo articolo.

  • Creare un hub di notifica
  • Impostare le credenziali

Se si desidera anche creare un nuovo spazio dei nomi per l'hub di notifica, vedere Gestire il bus di servizio con PowerShell.

La gestione degli hub di notifica non è supportata direttamente tramite i cmdlet inclusi con Azure PowerShell. L'approccio migliore da PowerShell consiste nel fare riferimento all'assembly Microsoft.Azure.NotificationHubs.dll. L'assembly viene distribuito con il pacchetto NuGet degli hub di notifica di Microsoft Azure.

Prerequisiti

Inclusione di un riferimento all'assembly .NET per il bus di servizio

La gestione degli hub di notifica di Azure non è ancora inclusa con i cmdlet PowerShell di Azure PowerShell. Per eseguire il provisioning degli hub di notifica, è possibile utilizzare il client .NET nel pacchetto NuGet degli hub di notifica di Microsoft Azure.

Innanzitutto, assicurarsi che lo script sia in grado di individuare l'assembly Microsoft.Azure.NotificationHubs.dll , che viene installato con il pacchetto NuGet in un progetto Visual Studio. Per essere flessibile, lo script esegue i passaggi seguenti:

  1. Determina il percorso in cui è stato richiamato.
  2. Scorre il percorso finché trova una cartella denominata packages. Questa cartella viene creata durante l'installazione dei pacchetti NuGet per Visual Studio.
  3. Cerca in modo ricorsivo nella cartella packages per trovare un assembly denominato Microsoft.Azure.NotificationHubs.dll.
  4. Fa quindi riferimento all'assembly in modo che i tipi siano disponibili per un uso successivo.

Di seguito viene illustrato in che modo questi passaggi vengono implementati in uno script di PowerShell:

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

Creare la classe NamespaceManager

Per eseguire il provisioning degli hub di notifica creare un'istanza della classe NamespaceManager dall’SDK.

È possibile usare il cmdlet Get-AzureSBAuthorizationRule incluso con Azure PowerShell per recuperare una regola di autorizzazione usata per fornire una stringa di connessione. Viene archiviato un riferimento all'istanza di NamespaceManager nella variabile $NamespaceManager. Si usa $NamespaceManager per eseguire il provisioning di un hub di notifica.

$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 di un nuovo hub di notifica

Per eseguire il provisioning di un nuovo hub di notifica, utilizzare l’ API .NET per Hub di notifica.

In questa parte dello script vengono impostate quattro variabili locali.

  1. $Namespace: impostare sul nome dello spazio dei nomi in cui si desidera creare un hub di notifica.
  2. $Path: impostare il percorso sul nome del nuovo hub di notifica. Ad esempio, "MyHub".
  3. $WnsPackageSid: impostare sul SID di pacchetto per l'App di Windows dal Windows Dev Center.
  4. $WnsSecretkey: impostare sulla chiave privata per l'App di Windows dal Windows Dev Center.

Queste variabili vengono utilizzate per connettersi allo spazio dei nomi e creare un nuovo Hub di notifica configurato per gestire le notifiche Windows Notification Services (WNS) con credenziali WNS per un’applicazione Windows. Per informazioni su come ottenere il SID di pacchetto e la chiave privata, vedere l’esercitazione Introduzione agli Hub di notifica .

  • Il frammento di script utilizza l’oggetto NamespaceManager da controllare per verificare se l'Hub di notifica è identificato da $Path esiste.
  • Se non esiste, lo script crea un NotificationHubDescription con credenziali WNS e lo passa alla classe NamespaceManager, metodo CreateNotificationHub.
$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."
}

Risorse aggiuntive

Sono disponibili per il download anche alcuni script predefiniti: