简体   繁体   中英

Microsoft Azure Provisioning JSON Template Output in Powershell

Hello Stackoverflow Community,

I have a problem with Microsoft Azure Provisioning, I am trying to access SharedAccessPolicyKeys for Resources like IoT-Hubs or Event-Hubs. I am trying this with listKeys function and output these inside the template JSON file:

    "outputs": {
"hubKeys": {
  "value": "[listKeys(resourceId('Microsoft.Devices/IotHubs', parameters('hubName')), '2016-02-03')]",
  "type": "object"
}

}

When I output the returned Object in Windows Powershell it looks like this:

    Type                       : Array
    IsReadOnly                 : False
    HasValues                  : True
    First                      : {keyName, primaryKey, secondaryKey, rights}
    Last                       : {keyName, primaryKey, secondaryKey, rights}
    Count                      : 5
    Parent                     : {{
                                   "keyName": "iothubowner",
                                   "primaryKey": "dZVFGkIysIgVRKjxlZsCWdk6KGa4rpBFlY6BOLmaiD8=",
                                   "secondaryKey": "HtRYETAdgja/TBSS3sVTshKaGzZWMLbZC6GR60emSV4=",
                                   "rights": "RegistryWrite, ServiceConnect, DeviceConnect"
                                 } {
                                   "keyName": "service",
                                   "primaryKey": "DGOujP2tBTiTTdKxukTx7umeYFFlDEhoih7fb0tP3i8=",
                                   "secondaryKey": "B+6j1nfEc59GAeJQNakNKolTBoR9kc5W+TUNzRXmDpc=",
                                   "rights": "ServiceConnect"
                                 } {
                                   "keyName": "device",
                                   "primaryKey": "qxmRJVH0yVhSkLEz8JaHhtDJaDofpw4SEKkZNlBwp7c=",
                                   "secondaryKey": "RhUuME9EnnUsE2sixswaiTofKsVVfCQNIllwkHgY/8A=",
                                   "rights": "DeviceConnect"
                                 } {
                                   "keyName": "registryRead",
                                   "primaryKey": "pEpHrL4amd9+7pvl6uCiYHL3rZhxV76tZ1P9bERO6Xc=",
                                   "secondaryKey": "6h4UBKd4WPkdpUfl0Hi3G5YKgB3LmtDMbgXDYx3eKrk=",
                                   "rights": "RegistryRead"
                                 } {
                                   "keyName": "registryReadWrite",
                                   "primaryKey": "HpCxKVa1686A8vOfNVBUzYSe2YJmKIwwAzxUh5DokuY=",
                                   "secondaryKey": "PGeYYID9y6cClqGD1rl4koLNySc7kOGK6VuNlBiwqmo=",
                                   "rights": "RegistryWrite"
                                 }}
    Root                       : {value}
    Next                       : 
    Previous                   : 
    Path                       : value
    LineNumber                 : 0
    LinePosition               : 0
    AllowNew                   : True
    AllowEdit                  : True
    AllowRemove                : True
    SupportsChangeNotification : True
    SupportsSearching          : False
    SupportsSorting            : False
    IsSorted                   : False
    SortProperty               : 
    SortDirection              : Ascending
    IsFixedSize                : False
    SyncRoot                   : System.Object
    IsSynchronized             : False

My Question: Can anyone tell me how to access the "primaryKey" in the different "keyName" Objects? In particular I need the PrimaryKey for "service".

I can print the Object with

    $Key = New-AzureRmResourceGroupDeployment (deleted parameters for this post)
    Write-Output $Key.Outputs.hubKeys

I already tried things like $Key.Outputs.hubKeys.value.Parents.values.... and countless other ways. Does anyone know how to get the Value?

Thanks, Arno

The sample here illustrates one way to achieve this. The ARM template creates an IoT Hub and and Azure Stream Analytics job that connects to the hub using the generated key values.

These snippets summarize the key pieces:

/* Create IoT Hub */
{
  "apiVersion": "2016-02-03",
  "type": "Microsoft.Devices/IotHubs",
  "name": "[variables('iotHubName')]",
  "location": "[resourceGroup().location]",
  "sku": "[parameters('iotHubSku')]"
},

/* Part of the ASA definition */
"datasource": {
  "type": "Microsoft.Devices/IotHubs",
  "properties": {
    "iotHubNamespace": "[variables('iotHubName')]",
    "sharedAccessPolicyName": "[variables('iotHubKeyName')]",
    "sharedAccessPolicyKey": "[listKeys(resourceId('Microsoft.Devices/IotHubs/Iothubkeys', variables('iotHubName'), variables('iotHubKeyName')), '2016-02-03').primaryKey]",
    "consumerGroupName": "[variables('archiveJobConsumerGroupName')]"
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM