如何在arm模板

时间:2016-11-28 11:05:26

标签: powershell azure azure-powershell azure-notificationhub arm-template

我使用以下azuredeploy.json文件在Azure云上设置通知中心。

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "Gcm.GoogleApiKey": {
            "type": "string",
            "metadata": {
                "description": "Google Cloud Messaging API Key"
            },
            "defaultValue": "AIzaSyAyp9MernKgMS3wFNM3yNWByiP-TaGrqEg"
        },
        "APNS.Certificate": {
            "type": "string",
            "metadata": {
                "description": "A certificate (in base 64 format) provided by Apple on the iOS Provisioning Portal"
            },
            "defaultValue": ""
        },
        "APNS.certificateKey": {
            "type": "string",
            "metadata": {
                "description": "The Certificate Key provided by the iOS Provisioning Portal when registering the application"
            },
            "defaultValue": "ce469bf21dfa7b9d595d4999bfaca8a94ea47e46"
        },
        "APNS.endpoint": {
            "type": "string",
            "metadata": {
                "description": "The APNS endpoint to which our service connects. This is one of two values: gateway.sandbox.push.apple.com for the sandbox endpoint or gateway.push.apple.com, for the production endpoint. Any other value is invalid."
            },
            "allowedValues": [
                "gateway.sandbox.push.apple.com",
                "gateway.push.apple.com"
            ],
            "defaultValue": "gateway.push.apple.com"
        }
    },
    "variables": {
        "hubVersion": "[providers('Microsoft.NotificationHubs', 'namespaces').apiVersions[0]]",
        "notificationHubNamespace": "[concat('hubv2', uniqueString(resourceGroup().id))]",
        "notificationHubName": "notificationhub"
    },
    "resources": [
        {
            "name": "[variables('NotificationHubNamespace')]",
            "location": "[resourceGroup().location]",
            "type": "Microsoft.NotificationHubs/namespaces",
            "apiVersion": "[variables('hubVersion')]",
            "comments": "Notification hub namespace",
            "properties": {
                "namespaceType": "NotificationHub"
            },
            "resources": [
                {
                    "name": "[concat(variables('NotificationHubNamespace'),'/',variables('NotificationHubName'))]",
                    "location": "[resourceGroup().location]",
                    "type": "Microsoft.NotificationHubs/namespaces/notificationHubs",
                    "apiVersion": "[variables('hubVersion')]",
                    "properties": {
                        "GcmCredential": {
                            "properties": {
                                "googleApiKey": "[parameters('Gcm.GoogleApiKey')]",
                                "gcmEndpoint": "https://android.googleapis.com/gcm/send"
                            }
                        }
                    },
                    "dependsOn": [
                        "[variables('NotificationHubNamespace')]"
                    ]
                }
            ]
        }
    ],
    "outputs": {
    }
}

现在我尝试使用以下代码段设置apple推送通知服务:

"apnsCredential": {
              "properties": {
                "apnsCertificate": "[parameters('APNS.Certificate')]",
                "certificateKey": "[parameters('APNS.certificateKey')]",
                "endpoint": " gateway.sandbox.push.apple.com or gateway.push.apple.com",
              }
            }

通过上述更改,我使用powershell命令提示符执行了Deploy-AzureResourceGroup.ps1,并在执行它时收到错误消息“Bad Request”

任何人都可以帮我解决这个问题。

4 个答案:

答案 0 :(得分:1)

添加适当的APNS.Certificate和APNS.certificateKey。 尝试验证您的详细信息失败了,因此请求不好。 您需要基本64格式的APNS.Certificate。

<强> APNS.Certificate:

这是基本64字符串格式的Apple推送通知证书。

您可以使用PowerShell转换这样的证书(然后从输出文件'MyPushCert.txt'复制密钥并使用它。):

$fileContentBytes = get-content ‘Apple_Certificate.p12’ -Encoding Byte

[System.Convert]::ToBase64String($fileContentBytes) | Out-File ‘MyPushCert.txt’

<强> APNS.certificateKey:

这是您导出证书时指定的密码。(您在创建证书时在Apple上创建的密码。)

答案 1 :(得分:0)

如果不了解更多有关您的环境/设置的信息,就不可能确切地知道是什么造成了这种情况。根据{{​​3}},一个可能的问题可能是您的密码有多强:

  

几个小时后拔出头发,没有得到任何东西   除了“错误请求”之外,我终于想到使用更强的密码   而不是“传递@ word1”。我会被愚弄,它有效。不仅如此,而且   使用Azure Resource Manager进行配置是异步的,因此您的   因为虚拟机,脚本比以前更快完成了很多工作   并行提供。

帖子建议通过this post

答案 2 :(得分:0)

我不确定您是否应该为模板动态设置apiVersion。它们根据您的部署情况而有所不同。

请参阅Best Practices

  

避免为资源类型的API版本使用参数或变量。资源属性和值可能因版本号而异。当API版本设置为参数或变量时,代码编辑器中的IntelliSense无法确定正确的模式。相反,在模板中对API版本进行硬编码。

通知中心的正确apiVersion似乎是2015-04-01https://github.com/Azure/azure-resource-manager-schemas/blob/master/schemas/2015-04-01/Microsoft.NotificationHubs.json

答案 3 :(得分:0)

我不得不使用PowerShell来解决这个问题。 部分意见来自这里:https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-deploy-and-manage-powershell

以下是本地测试并正在运行的脚本。 使用了Microsoft.Azure.NotificationHubs -Version 1.0.9。 我们采用VSTS版本作为使用ARM模板创建Notification Hub之后的PowerShell任务/步骤之一。

Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionName my-subscription-here

Write-Host "Begin process..."

try
{
    # Make sure to reference the latest version of Microsoft.Azure.NotificationHubs.dll
    Write-Host "Adding the [Microsoft.Azure.NotificationHubs.dll] assembly to the script...";
    $scriptPath = Split-Path (Get-Variable MyInvocation -Scope 0).Value.MyCommand.Path;
    $packagesFolder = $scriptPath + "\packs";
    Write-Host $packagesFolder;
    $assembly = Get-ChildItem $packagesFolder -Include "Microsoft.Azure.NotificationHubs.dll" -Recurse;
    write-Host $assembly.FullName;
    Add-Type -Path $assembly.FullName;

    Write-Host "The [Microsoft.Azure.NotificationHubs.dll] assembly has been successfully added to the script.";

    # Create requered variables
    $HubNamespace = "hub-namespace";
    $HubName = "hub-name";
    $ResourceGroup = "resource-group";

    $GcmApiKey = "api key here";
    # Possible values: gateway.push.apple.com, gateway.sandbox.push.apple.com
    $ApnsEndpoint = "gateway.push.apple.com";
    # A certificate (in base 64 format) provided by Apple on the iOS Provisioning Portal
    $ApnsCertificate = "base 64 certificate here";
    $ApnsCertificateKey = "certificate key/password here";

    $GcmCredential = New-Object -TypeName Microsoft.Azure.NotificationHubs.GcmCredential -ArgumentList $GcmApiKey;
    $ApnsCredential = New-Object -TypeName Microsoft.Azure.NotificationHubs.ApnsCredential;
    $ApnsCredential.Endpoint = $ApnsEndpoint;
    $ApnsCredential.ApnsCertificate = $ApnsCertificate;
    $ApnsCredential.CertificateKey = $ApnsCertificateKey;

    # Query the namespace
    $FoundNamespaces = Get-AzureRmNotificationHubsNamespace -Namespace $HubNamespace -ResourceGroup $ResourceGroup

    # Check if the namespace already exists
    if ($FoundNamespaces -and $FoundNamespaces.Length -eq 1)
    {
        $CurrentNamespace = $FoundNamespaces[0];
        Write-Host "The namespace [$HubNamespace] in the [$($CurrentNamespace.Location)] region was found.";

        $HubListKeys = Get-AzureRmNotificationHubListKeys -Namespace $HubNamespace -ResourceGroup $ResourceGroup -NotificationHub $HubName -AuthorizationRule DefaultFullSharedAccessSignature;
        # Check to see if the Notification Hub exists
        if ($HubListKeys)
        {
            # Create the NamespaceManager object used to update the notification hub
            Write-Host "Creating a NamespaceManager object for the [$HubNamespace] namespace...";
            $NamespaceManager = [Microsoft.Azure.NotificationHubs.NamespaceManager]::CreateFromConnectionString($HubListKeys.PrimaryConnectionString);
            Write-Host "NamespaceManager object for the [$HubNamespace] namespace has been successfully created.";

            # Update notification hub with new details
            Write-Host "The [$Path] notification hub already exists in the [$HubNamespace] namespace."  ;
            $NHDescription = $NamespaceManager.GetNotificationHub($HubName);
            $NHDescription.GcmCredential = $GcmCredential;
            $NHDescription.ApnsCredential = $ApnsCredential;
            $NHDescription = $NamespaceManager.UpdateNotificationHub($NHDescription);
            Write-Host "The [$HubName] notification hub was updated";
        }
        else
        {
            Write-Host "The [$HubName] notification hub does not exist."
        }
    }
    else
    {
        Write-Host "The [$HubNamespace] namespace does not exist."
    }
}
catch [System.Exception]
{
    Write-Error($_.Exception.Message)
}

希望能有所帮助。