Skip to content

Instantly share code, notes, and snippets.

@josuemb
Last active September 4, 2021 01:59
Show Gist options
  • Save josuemb/d0d627a9e09187c36ec9790e7ca4dff3 to your computer and use it in GitHub Desktop.
Save josuemb/d0d627a9e09187c36ec9790e7ca4dff3 to your computer and use it in GitHub Desktop.
This function helps you to get a list of all Cosmos DB accounts in some Azure Context and get the last time Accounts Keys has been updated and the number of keys updated.
<#
.SYNOPSIS
This function helps you to get a list of all Cosmos DB accounts in some Azure Context and get the last time Accounts Keys has been updated and the number of keys updated.
.DESCRIPTION
It depends on Az PowerShell module, then it should be installed and you need to be logged into the account.
To install and know more about Az PowerShell module, see: https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-6.3.0#overview
And then to authenticate you can use: Connect-AzAccount.
If you want to use this function from Azure PowerShell then you don-t need to install or login.
.PARAMETER startTime
Optional. It indicates the start time to look for Cosmos DB Key updates.
By default, it is 1 year ago.
You can use any valid date or just doing something like -startTime (Get-Date).AddMonths(-3)
.EXAMPLE
Import function to the current PowerShell session:
PS C:\> . C:\temp\PowerShellFunctions\Get-LastUpdateCosmosDBKeys.ps1
Where C:\temp\ is the path where the file is.
.EXAMPLE
Call without parameters:
PS C:\> Get-LastUpdateCosmosDBKeys
ResourceName ResourceId LastUpdate ChangedKeys
------------ ---------- ---------- -----------
jmbuserspoc /subscriptions/subscriptionId/resourceGroups/group1/providers/Microsoft.DocumentDb/databaseAccounts/database1 0
jmbcosmosnotebookdemo /subscriptions/subscriptionId/resourceGroups/group2/providers/Microsoft.DocumentDb/databaseAccounts/database2 8/31/2021 11:47:00 PM 1
It will get all Cosmos DB Key Updates from Current Context from the last year.
.EXAMPLE
Call with -startTime parameter:
PS C:\> Get-LastUpdateCosmosDBKeys -startTime (Get-Date).AddMonths(-3)
ResourceName ResourceId LastUpdate ChangedKeys
------------ ---------- ---------- -----------
jmbuserspoc /subscriptions/subscriptionId/resourceGroups/group1/providers/Microsoft.DocumentDb/databaseAccounts/database1 0
jmbcosmosnotebookdemo /subscriptions/subscriptionId/resourceGroups/group2/providers/Microsoft.DocumentDb/databaseAccounts/database2 8/31/2021 11:47:00 PM 1
It will get all Cosmos DB Key Updates for the last 3 months.
.EXAMPLE
Call with -startTime parameter:
PS C:\> Get-LastUpdateCosmosDBKeys -startTime (Get-Date).AddMonths(-3)
ResourceName ResourceId LastUpdate ChangedKeys
------------ ---------- ---------- -----------
jmbuserspoc /subscriptions/subscriptionId/resourceGroups/group1/providers/Microsoft.DocumentDb/databaseAccounts/database1 0
jmbcosmosnotebookdemo /subscriptions/subscriptionId/resourceGroups/group2/providers/Microsoft.DocumentDb/databaseAccounts/database2 8/31/2021 11:47:00 PM 1
It will get all Cosmos DB Key Updates for Current Context from the last 3 months.
.EXAMPLE
Call with ContextName parameter:
PS C:\> Get-LastUpdateCosmosDBKeys -ContextName 'My Context Name'
ResourceName ResourceId LastUpdate ChangedKeys
------------ ---------- ---------- -----------
jmbuserspoc /subscriptions/subscriptionId/resourceGroups/group1/providers/Microsoft.DocumentDb/databaseAccounts/database1 0
jmbcosmosnotebookdemo /subscriptions/subscriptionId/resourceGroups/group2/providers/Microsoft.DocumentDb/databaseAccounts/database2 8/31/2021 11:47:00 PM 1
It will get all Cosmos DB Key Updates for context with Name 'My Context Name' from the last year.
To check all Available context for your credentials, execute: Get-AzContext -ListAvailable | Select-Object -Property Name
.EXAMPLE
Call with pipeline:
PS C:\> PS C:\> Get-AzContext -ListAvailable | Get-LastUpdateCosmosDBKeys
ResourceName ResourceId LastUpdate ChangedKeys
------------ ---------- ---------- -----------
jmbuserspoc /subscriptions/subscriptionId/resourceGroups/group1/providers/Microsoft.DocumentDb/databaseAccounts/database1 0
jmbcosmosnotebookdemo /subscriptions/subscriptionId/resourceGroups/group2/providers/Microsoft.DocumentDb/databaseAccounts/database2 8/31/2021 11:47:00 PM 1
It will get all Cosmos DB Key Updates for all available contexts from the last year.
To check all Available context for your credentials, execute: Get-AzContext -ListAvailable | Select-Object -Property Name
.NOTES
Author: Josué Martínez Buenrrostro
Twitter: @josuemb
.LINK
https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az
https://docs.microsoft.com/en-us/powershell/azure/context-persistence
https://docs.microsoft.com/en-us/powershell/module/az.accounts/connect-azaccount
https://docs.microsoft.com/en-us/powershell/module/az.accounts/get-azcontext
#>
function Get-LastUpdateCosmosDBKeys {
[CmdletBinding()]
param (
[Parameter(ValueFromPipelineByPropertyName = $true)]
[Alias('Name')]
[string] $ContextName = $null,
[DateTime] $StartTime = (Get-Date).AddYears(-1)
)
PROCESS {
if ($ContextName) {
$context = Get-AzContext -Name $ContextName
}
else {
$context = Get-AzContext
}
$subscriptionName = $context.Subscription.Name
$updates = [System.Collections.ArrayList]@()
Write-Progress -Activity 'Get-LastUpdateCosmosDBKeys' -Status "Getting Cosmos DB Accounts from Subscription ""$subscriptionName"""
$cosmosDBAccounts = Get-AzResource -ResourceType 'Microsoft.DocumentDB/databaseAccounts' -DefaultProfile $context
$currentCosmosDBAccount = 0
$totalCosmosDBAccounts = ($cosmosDBAccounts | Measure-Object).Count
foreach ($cosmosDBAccount in $cosmosDBAccounts) {
Write-Progress -Activity 'Get-LastUpdateCosmosDBKeys' -Status "Getting last Key updates from Cosmos DB Accounts of Subscription ""$subscriptionName""" -PercentComplete ((($currentCosmosDBAccount++) / $totalCosmosDBAccounts) * 100)
$metricsInfo = Get-AzMetric -MetricName UpdateAccountKeys -ResourceId $cosmosDBAccount.ResourceId -StartTime $StartTime -WarningAction Ignore -DefaultProfile $context
$metrics = $metricsInfo.Data
$lastUpdate = $null
$changedKeys = 0
if ($metrics) {
$lastUpdateMetric = $metrics | Where-Object -Property Count -GT 0 | Sort-Object -Property TimeStamp -Descending | Select-Object -First 1
$lastUpdate = $lastUpdateMetric.TimeStamp
$changedKeys = $lastUpdateMetric.Count
}
$update = New-Object psobject
$update | Add-Member -MemberType NoteProperty -Name 'ResourceName' -Value $cosmosDBAccount.ResourceName
$update | Add-Member -MemberType NoteProperty -Name 'ResourceId' -Value $cosmosDBAccount.ResourceId
$update | Add-Member -MemberType NoteProperty -Name 'LastUpdate' -Value $lastUpdate
$update | Add-Member -MemberType NoteProperty -Name 'ChangedKeys' -Value $changedKeys
$updates += $update
}
return $updates
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment