Skip to content

Instantly share code, notes, and snippets.

@petevb
Last active January 26, 2021 11:35
Show Gist options
  • Save petevb/be0799e89d196a140c24c44b5e0b4dcd to your computer and use it in GitHub Desktop.
Save petevb/be0799e89d196a140c24c44b5e0b4dcd to your computer and use it in GitHub Desktop.
az cli samples #azcli #azure #bash
# https://docs.microsoft.com/en-us/azure/azure-monitor/app/powershell#set-the-data-retention
Param(
[Parameter(Mandatory = $True)]
[string]$SubscriptionId,
[Parameter(Mandatory = $True)]
[string]$ResourceGroupName,
[Parameter(Mandatory = $True)]
[string]$Name,
[Parameter(Mandatory = $True)]
[string]$RetentionInDays
)
$ErrorActionPreference = 'Stop'
if (-not (Get-Module Az.Accounts)) {
Import-Module Az.Accounts
}
$azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
if (-not $azProfile.Accounts.Count) {
Write-Error "Ensure you have logged in before calling this function."
}
$currentAzureContext = Get-AzContext
$profileClient = New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient($azProfile)
$token = $profileClient.AcquireAccessToken($currentAzureContext.Tenant.TenantId)
$UserToken = $token.AccessToken
$RequestUri = "https://management.azure.com/subscriptions/$($SubscriptionId)/resourceGroups/$($ResourceGroupName)/providers/Microsoft.Insights/components/$($Name)?api-version=2015-05-01"
$Headers = @{
"Authorization" = "Bearer $UserToken"
"x-ms-client-tenant-id" = $currentAzureContext.Tenant.TenantId
}
## Get Component object via ARM
$GetResponse = Invoke-RestMethod -Method "GET" -Uri $RequestUri -Headers $Headers
## Update RetentionInDays property
if($($GetResponse.properties | Get-Member "RetentionInDays"))
{
$GetResponse.properties.RetentionInDays = $RetentionInDays
}
else
{
$GetResponse.properties | Add-Member -Type NoteProperty -Name "RetentionInDays" -Value $RetentionInDays
}
## Upsert Component object via ARM
$PutResponse = Invoke-RestMethod -Method "PUT" -Uri "$($RequestUri)" -Headers $Headers -Body $($GetResponse | ConvertTo-Json) -ContentType "application/json"
$PutResponse
# app-insights is in preview so you may need:
# https://docs.microsoft.com/en-gb/cli/azure/ext/application-insights/monitor/app-insights?view=azure-cli-latest

az extension add -n application-insights

# tried these but the data they return doesn't include retentionInDays like powershell allows.
az monitor app-insights component show -g $rg

az monitor app-insights component show -a ttc-eun-pre -g ttc-pre -o json

az monitor app-insights component show -a ttc-eun-pre -g ttc-pre -o json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment