Is there is a way to get the primary key of an Azure DocumentDB using PowerShell?
Thanks,
I could find a better way to List Keys of a any azure resource.
Example: for document DB- you can use the following script
$keys = Invoke-AzureRmResourceAction -Action listKeys -ResourceType "Microsoft.DocumentDb/databaseAccounts" -ApiVersion "2015-04-08" -ResourceGroupName "<resource-group-name>" -Name "<database-account-name>"
For more info, Please visit msdn article. https://learn.microsoft.com/en-us/azure/documentdb/documentdb-manage-account-with-powershell
You can use the below powershell script to create the DocumentDB account and retrieve the key.
You can modifty it if you just want to retrieve the key.
Let me know if this helps or you require help. Happy to help :)
#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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With