使用 PowerShell 部署和管理通知中心Deploy and manage notification hubs using PowerShell

概述Overview

本文说明如何使用 PowerShell 来创建和管理 Azure 通知中心。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

如果还需要为通知中心创建新的服务总线命名空间,请参阅使用 PowerShell 管理服务总线If you also need to create a new service bus namespace for your notification hubs, see Manage Service Bus with PowerShell.

不支持直接使用 Azure PowerShell 随附的 cmdlet 来管理通知中心。Managing Notifications Hubs is not supported directly by the cmdlets included with Azure PowerShell. 在 PowerShell 中,最佳方法是引用 Microsoft.Azure.NotificationHubs.dll 程序集。The best approach from PowerShell is to reference the Microsoft.Azure.NotificationHubs.dll assembly. 该程序集是随 Microsoft Azure 通知中心 NuGet 包一起分发的。The assembly is distributed with the Microsoft Azure Notification Hubs NuGet package.

先决条件Prerequisites

  • Azure 订阅。An Azure subscription. Azure 是基于订阅的平台。Azure is a subscription-based platform. 有关如何获取订阅的详细信息,请参阅购买选项或 [1 元人民币的试用订阅]。For more information about obtaining a subscription, see Purchase Options or [1RMB Trial].
  • 配备 Azure PowerShell 的计算机。A computer with Azure PowerShell. 有关说明,请参阅 安装和配置 Azure PowerShellFor instructions, see Install and configure Azure PowerShell.
  • 大致了解 PowerShell 脚本、NuGet 包和 .NET Framework。A general understanding of PowerShell scripts, NuGet packages, and the .NET Framework.

包含对适用于服务总线的 .NET 程序集的引用Including a reference to the .NET assembly for Service Bus

Azure PowerShell 中的 PowerShell cmdlet 尚不支持管理 Azure 通知中心。Managing Azure Notification Hubs is not yet included with the PowerShell cmdlets in Azure PowerShell. 若要预配通知中心,可以使用 Azure 通知中心 NuGet 包中提供的 .NET 客户端。To provision notification hubs, you can use the .NET client provided in the Azure Notification Hubs NuGet package.

首先,请确保脚本可以找到 Microsoft.Azure.NotificationHubs.dll 程序集,该程序集在 Visual Studio 项目中以 NuGet 包的形式安装。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. 遍历路径直到找到名为 packages的文件夹为止。Traverses the path until it finds a folder named packages. 此文件夹是在为 Visual Studio 项目安装 NuGet 包时创建的。This folder is created when you install NuGet packages for Visual Studio projects.
  3. 以递归方式在 packages 文件夹中搜索名为 Microsoft.Azure.NotificationHubs.dll 的程序集。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.

下面说明如何在 PowerShell 脚本中实现这些步骤: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.")
}

创建 NamespaceManagerCreate the NamespaceManager class

若要预配通知中心,请从 SDK 创建 NamespaceManager 类的实例。To provision Notification Hubs, create an instance of the NamespaceManager class from the SDK.

可以使用 Azure PowerShell 随附的 Get-AzureSBAuthorizationRule cmdlet 来检索用于提供连接字符串的授权规则。You can use the Get-AzureSBAuthorizationRule cmdlet included with Azure PowerShell to retrieve an authorization rule that's used to provide a connection string. NamespaceManager 实例的引用存储于 $NamespaceManager 变量中。A reference to the NamespaceManager instance is stored in the $NamespaceManager variable. $NamespaceManager 用于设置通知中心。$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

若要预配新的通知中心,请使用 通知中心的 .NET APITo 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:将此变量设置为要在其中创建通知中心的命名空间的名称。$Namespace: Set this to the name of the namespace where you want to create a notification hub.
  2. $Path:将此路径设置为新通知中心的名称。$Path: Set this path to the name of the new notification hub. 例如“MyHub”。For example, "MyHub".
  3. $WnsPackageSid:从 Windows 开发人员中心将此变量设置为 Windows 应用的包 SID。$WnsPackageSid: Set this to the package SID for your Windows App from the Windows Dev Center.
  4. $WnsSecretkey:从 Windows 开发人员中心将此变量设置为 Windows 应用的机密密钥。$WnsSecretkey: Set this to the secret key for your Windows App from the Windows Dev Center.

这些变量可用于连接命名空间,以及创建配置为使用 Windows 应用 Windows 通知中心 (WNS) 凭据处理 WNS 通知的新通知中心。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. 有关获取包 SID 和机密密钥的信息,请参阅通知中心入门教程。For information on obtaining the package SID and secret key see, the Getting Started with Notification Hubs tutorial.

  • 脚本代码段使用 NamespaceManager 对象来检查 $Path 标识的通知中心是否存在。The script snippet uses the NamespaceManager object to check to see if the Notification Hub identified by $Path exists.
  • 如果不存在,则脚本会使用 WNS 凭据创建 NotificationHubDescription,并将其传递给 NamespaceManagerCreateNotificationHub 方法。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: