Skip to content

Instantly share code, notes, and snippets.

@SQLDBAWithABeard
Last active May 30, 2022 12:17
Show Gist options
  • Save SQLDBAWithABeard/6fbf54ad19ceb33ff3472a58e78f21fa to your computer and use it in GitHub Desktop.
Save SQLDBAWithABeard/6fbf54ad19ceb33ff3472a58e78f21fa to your computer and use it in GitHub Desktop.
To show some Azure Graph things
$rgwithwrongtags = $results #| where resourcebilling -eq 'somedefaultvalue'
$count = $rgwithwrongtags.Count
foreach ($resource in $rgwithwrongtags) {
$context = Get-AzContext
$excludedtypes = 'microsoft.compute/virtualmachines/extensions','microsoft.network/privatednszones/virtualnetworklinks','microsoft.network/privatednszones'
if ($resource.type -notin $excludedtypes) {
Set-BillingTag -resource $resource -billingtag $resource.rgbilling
}
else {
$message = "We will not set tags on the resource {0} in Resource Group {1} as it is a {2}" -f $resource.name, $resource.resourceGroup, $resource.type
Write-PSFMessage $message -Level Significant
}
$count --
$message = "We have {0} left to do" -f $count
Write-PSFMessage $message -Level Significant
}
function Set-BillingTag {
param(
$resource,
$billingtag
)
$context = Get-AzContext
if ($resource.subscriptionId -ne $context.Subscription.Id) {
Set-AzContext -SubscriptionId $resource.subscriptionId
$context = Get-AzContext
$message = "swapped to {0} subscription" -f $context.Subscription.Name
Write-PSFMessage $message -Level Significant -Verbose
}
try {
$message = "We will set the resource {0} in Resource Group {1} to have the billing tag {2} instead of {3}" -f $resource.name, $resource.resourceGroup, $billingtag, $resource.resourcebilling
Write-PSFMessage $message -Level Significant
$tags = (Get-AzResource -ResourceId $resource.resourceId).Tags
$tags.billing = $billingtag
Set-AzResource -ResourceId $resource.resourceId -Tag $tags -Confirm:$false -Force -ErrorAction Stop | Out-Null
}
catch {
$message = "Failed to set the tags on resource {0} in Resource Group {1} type {2}" -f $resource.name, $resource.resourceGroup, $resource.type
Write-PSFMessage $message -Level Warning -ErrorRecord $_
}
$rg = Get-AzResourceGroup -Name $resource.resourceGroup
$tags = $rg.Tags
# cos we need default tags el;se the policy bombs us out
if($null -eq $tags.application){
$tags.add('application','unknown')
}
if($null -eq $tags.creator){
$tags.add('creator','unknown')
}
if($null -eq $tags.billing){
$tags.add('billing','unknown')
}
if($tags.billing -ne $billingtag){
try {
$message = "We will set the Resource Group {0} to have the billing tag {1} instead of {2}" -f $resource.resourceGroup, $billingtag, $resource.resourcebilling
Write-PSFMessage $message -Level Significant
$tags.billing = $billingtag
Set-AzResource -ResourceId $rg.resourceId -Tag $tags -Confirm:$false -Force -ErrorAction Stop | Out-Null
}
catch {
$message = "Failed to set the tags on Resource Group {0} to have the billing tag {1} instead of {2}" -f $resource.resourceGroup, $billingtag, $resource.resourcebilling
Write-PSFMessage $message -Level Warning -ErrorRecord $_
}
}else {
$message = "We do not need to set the tag on the Resource Group {0} " -f $resource.resourceGroup
Write-PSFMessage $message -Level Significant
}
}
# Install the Resource Graph module from PowerShell Gallery
## load a module
if(-not (Get-Module -Name Az.ResourceGraph -ListAvailable)){
Install-Module -Name Az.ResourceGraph
}
# resource groups tags and resources tags to excel
$query = @"
resources
| where ['type'] !in~ ('microsoft.compute/virtualmachines/extensions','microsoft.network/privatednszones/virtualnetworklinks','microsoft.network/privatednszones')
| where ['type'] !startswith "microsoft.network"
| join kind=inner (
resourcecontainers
| where type =~ 'microsoft.resources/subscriptions/resourcegroups'
//| where tags['billing'] == 'beards are ace'
| project subscriptionId, resourceGroup,rgtags=tags)
on subscriptionId, resourceGroup
//|limit 10 // for checking
| project-away subscriptionId1, resourceGroup1
| project subscriptionId,resourceGroup, name,rgbilling=rgtags['billing'], resourcebilling=tags['billing'], type,id
|where resourcebilling == ''
"@
$date = Get-Date -Format yyyyMMdd-HHmmss
$filename = './output/rgsandblankbillingtags-{0}.xlsx' -f $date
$pageSize = 1000
$iteration = 0
$searchParams = @{
Query = $query
First = $pageSize
}
$results = do {
$iteration += 1
Write-Verbose "Iteration #$iteration" -Verbose
$pageResults = Search-AzGraph @searchParams
$searchParams.Skip += $pageResults.Count
$pageResults
} while ($pageResults.Count -eq $pageSize)
$results.Count
($results | where resourcebilling -eq 'the default entry').Count
$results | Export-Excel -Path $filename -WorksheetName 'Rgs and Tags' -AutoSize -AutoFilter
Leave the gun. Take the cannoli.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment