【问题标题】:Get Azure DocumentDB Primary Key using PowerShell使用 PowerShell 获取 Azure DocumentDB 主键
【发布时间】:2019-10-07 17:45:08
【问题描述】:

有没有办法使用 PowerShell 获取 Azure DocumentDB 的主键?

谢谢,

【问题讨论】:

标签: powershell azure azure-cosmosdb azure-resource-manager


【解决方案1】:

我可以找到一种更好的方法来列出任何天蓝色资源的键。

示例:对于文档 DB- 您可以使用以下脚本

$keys = Invoke-AzureRmResourceAction -Action listKeys -ResourceType "Microsoft.DocumentDb/databaseAccounts" -ApiVersion "2015-04-08" -ResourceGroupName "<resource-group-name>" -Name "<database-account-name>"

欲了解更多信息, 请访问 msdn 文章。 https://docs.microsoft.com/en-us/azure/documentdb/documentdb-manage-account-with-powershell

【讨论】:

  • 如果要返回只读键而不是读写键,请将操作更改为readonlykeys
【解决方案2】:

您可以使用以下 powershell 脚本创建 DocumentDB 帐户并检索密钥。

如果你只是想检索密钥,你可以修改它。

如果这有帮助或者您需要帮助,请告诉我。乐于助人:)

#Input
$subName = "<subscription name>"
$rgName = "<resource group name>"
$docDBAccount = "<DocDB account name>"
Select-AzureRmSubscription -SubscriptionName $subName
$sub = Get-AzureRmSubscription -SubscriptionName $subName

#Get Azure AAD auth token
$clientId = "1950a258-227b-4e31-a9cf-717495945fc2"
$redirectUri = "urn:ietf:wg:oauth:2.0:oob"
$resourceClientId = "00000002-0000-0000-c000-000000000000"
$resourceAppIdURI = "https://management.core.windows.net/"
$authority = "https://login.windows.net/common"
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority,$false
$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $redirectUri, "Auto")
$header = $authresult.CreateAuthorizationHeader()
$tenants = Invoke-RestMethod -Method GET -Uri "https://management.azure.com/tenants?api-version=2014-04-01" -Headers @{"Authorization"=$header} -ContentType "application/json"
$tenant = $tenants.value.tenantId
$authority = [System.String]::Format("https://login.windows.net/{0}", $tenant)
$authContext = New-Object    "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority,$false
$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $redirectUri, "Auto")
$header = $authresult.CreateAuthorizationHeader()


#Get the account keys and dsi
$account = Get-AzureRmResource -ResourceType Microsoft.DocumentDb/databaseAccounts -ResourceName $docDBAccount -ResourceGroupName $rgName
$keysurl = [System.String]::Format("https://management.azure.com/subscriptions/{0}/resourcegroups/{1}/providers/Microsoft.DocumentDB/databaseAccounts/{2}/listKeys?api-version=2014-04-01", $sub.SubscriptionId, $rgName, $docDBAccount)
$keys = Invoke-RestMethod -Method POST -Uri $keysurl -Headers @{"Authorization"=$header} -ContentType "application/json"
$account.Properties.DocumentEndpoint
$keys.primaryMasterKey
$keys.secondaryMasterKey

【讨论】:

  • 嗨 Satish,我以前看过这个 PowerShell 脚本,但是如何使用 PowerShell 获取这些值?我正在尝试自动化我的部署。 $clientId = "1950a258-227b-4e31-a9cf-717495945fc2" $redirectUri = "urn:ietf:wg:oauth:2.0:oob" $resourceClientId = "00000002-0000-0000-c000-000000000000"
  • 您现在可以使用相同的 clientid 和 resourceclientid。它应该适合你。我们将来会有更好的体验。
【解决方案3】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 2020-04-21
相关资源
最近更新 更多