使用 PowerShell 获取 Azure DocumentDB 主键

Get Azure DocumentDB Primary Key using PowerShell

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

谢谢,

您可以使用以下 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

以下是解释上述值的文章:

How does Azure PowerShell work with username/password based auth?

https://blogs.msdn.microsoft.com/besidethepoint/2012/10/23/getting-started-with-azure-active-directory/

https://azure.microsoft.com/en-us/documentation/articles/active-directory-v2-protocols-oauth-code/

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

示例:对于文档 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