使用PowerShell获取Azure DocumentDB主键

A S*_*yam 2 powershell azure azure-resource-manager azure-cosmosdb

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

谢谢,

小智 5

您可以使用以下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
Run Code Online (Sandbox Code Playgroud)


小智 5

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

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

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

有关更多信息,请访问msdn文章。 https://docs.microsoft.com/zh-cn/azure/documentdb/documentdb-manage-account-with-powershell