Skip to content

Instantly share code, notes, and snippets.

@kinzakanwal
Last active May 25, 2023 11:52
Show Gist options
  • Save kinzakanwal/9e6613864df5c15ac045a59caf748ad0 to your computer and use it in GitHub Desktop.
Save kinzakanwal/9e6613864df5c15ac045a59caf748ad0 to your computer and use it in GitHub Desktop.
This gist referrers to the PowerShell script that is used to extract the count of azure resources across all the subscriptions in one tenant and then send the results via email.
workflow count_resource
{
inlineScript
{
#------------------- LOGIN TO AZURE -------------------#
try
{
"Logging in to Azure..."
Connect-AzAccount -Identity
}
catch {
Write-Error -Message $_.Exception
throw $_.Exception
}
#------------------- CONFIGURE EMAIL SERVER -------------------#
$Username ="username"
$Password = ConvertTo-SecureString "PasswordString" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $Username, $Password
$SMTPServer = "smtp.sendgrid.net"
$EmailFrom = "youremail.com"
[string[]]$EmailTo = "youremail.com"
$Subject = "Attached - Resource Count"
#------------------- CALCULATING COUNT -------------------#
$WebCount = 0
$DbCount = 0
$VmCount = 0
$ArrayOfSubscriptions = "Subscription1 ID","Subscription2 ID" , "Subscription3 ID","Subscription4 ID","Subscription5 ID"
# START OF LOOP
foreach ($element in $ArrayOfSubscriptions) {
echo $element
Set-AzContext -Subscription $element
class AzResourceGraphException : Exception {
[string] $additionalData
AzResourceGraphException($Message, $additionalData) : base($Message) {
$this.additionalData = $additionalData
}
}
#------------------- WEBSITES -------------------#
try {
$resourceGraphQuery = "Resources | where type =~ 'Microsoft.Web/sites' | summarize count()"
$WebCount = Search-AzGraph -Subscription $element -Query $resourceGraphQuery -ErrorVariable grapherror -ErrorAction SilentlyContinue
if ($null -ne $grapherror.Length) {
$errorJSON = $grapherror.ErrorDetails.Message | ConvertFrom-Json
throw [AzResourceGraphException]::new($errorJSON.error.details.code, $errorJSON.error.details.message)
}
}
catch [AzResourceGraphException] {
Write-Host "An error on KQL query"
Write-Host $_.Exception.message
Write-Host $_.Exception.additionalData
}
catch {
Write-Host "An error occurred in the script"
Write-Host $_.Exception.message
}
#------------------- DATABASE -------------------#
try {
$resourceGraphQuery = "Resources | where type =~ 'Microsoft.Sql/Servers/databases' | summarize count()"
$DbCount = Search-AzGraph -Subscription $element -Query $resourceGraphQuery -ErrorVariable grapherror -ErrorAction SilentlyContinue
if ($null -ne $grapherror.Length) {
$errorJSON = $grapherror.ErrorDetails.Message | ConvertFrom-Json
throw [AzResourceGraphException]::new($errorJSON.error.details.code, $errorJSON.error.details.message)
}
}
catch [AzResourceGraphException] {
Write-Host "An error on KQL query"
Write-Host $_.Exception.message
Write-Host $_.Exception.additionalData
}
catch {
Write-Host "An error occurred in the script"
Write-Host $_.Exception.message
}
#------------------- VIRTUAL MACHINES -------------------#
try {
$resourceGraphQuery = "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | summarize count()"
$VmCount = Search-AzGraph -Subscription $element -Query $resourceGraphQuery -ErrorVariable grapherror -ErrorAction SilentlyContinue
if ($null -ne $grapherror.Length) {
$errorJSON = $grapherror.ErrorDetails.Message | ConvertFrom-Json
throw [AzResourceGraphException]::new($errorJSON.error.details.code, $errorJSON.error.details.message)
}
}
catch [AzResourceGraphException] {
Write-Host "An error on KQL query"
Write-Host $_.Exception.message
Write-Host $_.Exception.additionalData
}
catch {
Write-Host "An error occurred in the script"
Write-Host $_.Exception.message
}
# END OF LOOP
# ECHO RESULT IS DISPLAYED IN OUTPUT WINDOW OF YOUR RUNBOOK
echo "Web Count" $WebCount.count_
echo "Db Count" $DbCount.count_
echo "VM Count" $VmCount.count_
# COMBINE THE COUNTS FOR ALL SUBSCRIPTION TO GET THE COUNT FOR TENANT
$WebsiteCount = $WebsiteCount + $WebCount.count_
$DatabaseCount = $DatabaseCount + $DbCount.count_
$VirtualMachineCount = $VirtualMachineCount + $VmCount.count_
}
#------------------- SENDING EMAIL -------------------#
$Body = "Count of Azure Resources = "
Send-MailMessage -smtpServer $SMTPServer -Credential $credential -Usessl -Port 587 -from $EmailFrom -to $EmailTo -subject $Subject -Body "Count of Tenant Resources: <br/><br/><br/>Count of Azure Websites : $WebsiteCount <br/><br/><br/> Count of Azure Databases : $DatabaseCount <br/><br/><br/> Count of Azure VirtualMachines : $VirtualMachineCount " -BodyAsHtml
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment