From 5fd497d243b36ed4c0a1af476bf75cc83ad711e8 Mon Sep 17 00:00:00 2001 From: Alexander Nosov Date: Wed, 16 Dec 2020 19:31:30 -0800 Subject: [PATCH] Added EC calculation --- samples/manage/azure-hybrid-benefit/README.md | 4 +- .../ahb-usage-in-subscription.ps1 | 2 +- .../sql-license-usage.ps1 | 99 +++++++++++-------- 3 files changed, 61 insertions(+), 44 deletions(-) diff --git a/samples/manage/azure-hybrid-benefit/README.md b/samples/manage/azure-hybrid-benefit/README.md index 426a897fa2..d8b6b6cfe2 100644 --- a/samples/manage/azure-hybrid-benefit/README.md +++ b/samples/manage/azure-hybrid-benefit/README.md @@ -8,7 +8,7 @@ ms.date: 12/15/2020 # Overview -This script is provided to help you manage the SQL Server licenses that are consumed by the SQL Servers deployed to Azure. The script's output is a `sql-license-usage.csv` file with the consolidated SQL Server license usage by all SQL resources in the specific subscriptions or the entire account. The usage is broken down into the following categories of licenses: +This script is provided to help you manage the SQL Server licenses that are consumed by the SQL Servers deployed to Azure. The script's output is a `sql-license-usage.csv` file with the consolidated SQL Server license usage by all SQL resources in the specific subscriptions or the entire account. The usage is broken into the following license categories: - AHB Standard vCores - AHB Enterprise vCores @@ -58,6 +58,6 @@ If you need to scan a subset of the subscriptions, use the following steps: > [!NOTE] > - To paste the commands into the shell, use `Ctrl-Shift-V` on Windows or `Cmd-v` on MacOS. -> - The script will be uploaded directly to the home folder associated with your Cloud Shell session. +> - The `curl` command will copy the script directly to the home folder associated with your Cloud Shell session. > - The script will prompt for the resource group name and print a message when migration is completed. diff --git a/samples/manage/azure-hybrid-benefit/ahb-usage-in-subscription.ps1 b/samples/manage/azure-hybrid-benefit/ahb-usage-in-subscription.ps1 index 3694ec3c6c..1ddf88650b 100644 --- a/samples/manage/azure-hybrid-benefit/ahb-usage-in-subscription.ps1 +++ b/samples/manage/azure-hybrid-benefit/ahb-usage-in-subscription.ps1 @@ -28,7 +28,7 @@ Set-AzContext -SubscriptionId $SubcriptionId $total_std_vcores = 0 $total_ent_vcores = 0 -#Get all SQL databadses in the subscription +#Get all SQL databases in the subscription $databases = Get-AzSqlServer | Get-AzSqlDatabase # Get the databases with License Included and add to VCore count diff --git a/samples/manage/azure-hybrid-benefit/sql-license-usage.ps1 b/samples/manage/azure-hybrid-benefit/sql-license-usage.ps1 index eb9c439030..82881264d2 100644 --- a/samples/manage/azure-hybrid-benefit/sql-license-usage.ps1 +++ b/samples/manage/azure-hybrid-benefit/sql-license-usage.ps1 @@ -41,7 +41,13 @@ if ($args[0] -ne $null) { } [System.Collections.ArrayList]$usage = @() -$usage += ,(@("Subscription Name", "Subscription ID", "AHB Std vCores", "AHB Ent vCores", "PAYG Std vCores", "PAYG Ent vCores", "HADR Std vCores", "HADR Ent vCores", "Developer vCores", "Express vCores")) +if ($ec -eq $null){ + $usage += ,(@("Subscription Name", "Subscription ID", "AHB Std vCores", "AHB Ent vCores", "PAYG Std vCores", "PAYG Ent vCores", "HADR Std vCores", "HADR Ent vCores", "Developer vCores", "Express vCores")) +}else{ + $usage += ,(@("Subscription Name", "Subscription ID", "AHB ECs", "PAYG ECs", "AHB Std vCores", "AHB Ent vCores", "PAYG Std vCores", "PAYG Ent vCores", "HADR Std vCores", "HADR Ent vCores", "Developer vCores", "Express vCores")) +} +$total = [pscustomobject]@{ahb_std=0; ahb_ent=0; payg_std=0; payg_ent=0; hadr_std=0; hadr_ent=0; developer=0; express=0} + #Save the VM SKU table $VM_SKUs = Get-AzComputeResourceSku @@ -61,49 +67,49 @@ foreach ($sub in $subscriptions){ } # Total counts of different license types - $total = [pscustomobject]@{ahb_std=0; ahb_ent=0; payg_std=0; payg_ent=0; hadr_std=0; hadr_ent=0; developer=0; express=0} + $subtotal = [pscustomobject]@{ahb_std=0; ahb_ent=0; payg_std=0; payg_ent=0; hadr_std=0; hadr_ent=0; developer=0; express=0} #Get all logical servers $servers = Get-AzSqlServer - #Get all SQL databadse resources in the subscription + #Get all SQL database resources in the subscription $databases = $servers | Get-AzSqlDatabase - # Get the databases with License Included and add to VCore count + # Process the vCore-based databases foreach ($db in $databases ){ if ($db.SkuName -eq "ElasticPool") {continue} if ($db.LicenseType -eq "LicenseIncluded") { if ($db.Edition -eq "BusinessCritical") { - $total.ahb_ent += $db.Capacity + $subtotal.ahb_ent += $db.Capacity } elseif ($db.Edition -eq "GeneralPurpose") { - $total.ahb_std += $db.Capacity + $subtotal.ahb_std += $db.Capacity } }else{ if ($db.Edition -eq "BusinessCritical") { - $total.payg_ent += $db.Capacity + $subtotal.payg_ent += $db.Capacity } elseif ($db.Edition -eq "GeneralPurpose") { - $total.payg_std += $db.Capacity - }$table + $subtotal.payg_std += $db.Capacity + } } } #Get all SQL elastic pool resources in the subscription $pools = $servers | Get-AzSqlElasticPool - # Get the elastic pools with License Included and and add to VCore count + # Process the vCore-based elastic pools foreach ($pool in $pools){ if ($pool.LicenseType -eq "LicenseIncluded") { if ($pool.Edition -eq "BusinessCritical") { - $total.ahb_ent += $pool.Capacity + $subtotal.ahb_ent += $pool.Capacity } elseif ($pool.Edition -eq "GeneralPurpose") { - $total.ahb_std += $pool.Capacity + $subtotal.ahb_std += $pool.Capacity } }else{ if ($pool.Edition -eq "BusinessCritical") { - $total.payg_ent += $pool.Capacity + $subtotal.payg_ent += $pool.Capacity } elseif ($pool.Edition -eq "GeneralPurpose") { - $total.payg_std += $pool.Capacity + $subtotal.payg_std += $pool.Capacity } } } @@ -111,20 +117,20 @@ foreach ($sub in $subscriptions){ #Get all SQL managed instance resources in the subscription $instances = Get-AzSqlInstance - # Get the SQL managed instances with License Included and add to VCore count + # Process the SQL managed instances with License Included and add to VCore count foreach ($ins in $instances){ if ($ins.InstancePoolName -eq $null){ if ($ins.LicenseType -eq "LicenseIncluded") { if ($ins.Sku.Tier -eq "BusinessCritical") { - $total.ahb_ent += $ins.VCores + $subtotal.ahb_ent += $ins.VCores } elseif ($ins.Sku.Tier -eq "GeneralPurpose") { - $total.ahb_std += $ins.VCores + $subtotal.ahb_std += $ins.VCores } }else{ if ($ins.Edition -eq "BusinessCritical") { - $total.payg_ent += $pool.Capacity + $subtotal.payg_ent += $pool.Capacity } elseif ($ins.Edition -eq "GeneralPurpose") { - $total.payg_std += $ins.Capacity + $subtotal.payg_std += $ins.Capacity } } } @@ -133,19 +139,19 @@ foreach ($sub in $subscriptions){ #Get all instance pool resources in the subscription $ipools = Get-AzSqlInstancePool - # Get the instance pools with License Included and add to VCore count + # Process the instance pools foreach ($ip in $ipools){ if ($ip.LicenseType -eq "LicenseIncluded") { if ($ip.Edition -eq "BusinessCritical") { - $total.ahb_ent += $ip.VCores + $subtotal.ahb_ent += $ip.VCores } elseif ($ip.Edition -eq "GeneralPurpose") { - $total.ahb_std += $ip.VCores + $subtotal.ahb_std += $ip.VCores } }else{ if ($ip.Edition -eq "BusinessCritical") { - $total.payg_ent += $ip.Capacity + $subtotal.payg_ent += $ip.Capacity } elseif ($ip.Edition -eq "GeneralPurpose") { - $total.payg_std += $ip.Capacity + $subtotal.payg_std += $ip.Capacity } } } @@ -165,19 +171,19 @@ foreach ($sub in $subscriptions){ if ($ssis_ir.State -eq "Started"){ if ($ssis_ir.LicenseType -like "LicenseIncluded"){ if ($ssis_ir.Edition -like "Enterprise"){ - $total.ahb_ent += $vcpu.value + $subtotal.ahb_ent += $vcpu.value }elseif ($ssis_ir.Edition -like "Standard"){ - $total.ahb_std += $vcpu.value + $subtotal.ahb_std += $vcpu.value } }elseif ($data.license -like "BasePrice"){ if ($ssis_ir.Edition -like "Enterprise"){ - $total.payg_ent += $vcpu.value + $subtotal.payg_ent += $vcpu.value }elseif ($ssis_ir.Edition -like "Standard"){ - $total.payg_std += $vcpu.value + $subtotal.payg_std += $vcpu.value }elseif ($ssis_ir.Edition -like "Developer"){ - $total.developer += $vcpu.value + $subtotal.developer += $vcpu.value }elseif ($ssis_ir.Edition -like "Express"){ - $total.express += $vcpu.value + $subtotal.express += $vcpu.value } } } @@ -201,33 +207,44 @@ foreach ($sub in $subscriptions){ if ($data.license -like "DR"){ if ($data.sku -like "Enterprise"){ - $total.hadr_ent += $data.vcpus + $subtotal.hadr_ent += $data.vcpus }elseif ($data.sku -like "Standard"){ - $total.hadr_std += $data.vcpus + $subtotal.hadr_std += $data.vcpus } }elseif ($data.license -like "AHUB"){ if ($data.sku -like "Enterprise"){ - $total.ahb_ent += $data.vcpus + $subtotal.ahb_ent += $data.vcpus }elseif ($data.sku -like "Standard"){ - $total.ahb_std += $data.vcpus + $subtotal.ahb_std += $data.vcpus } }elseif ($data.license -like "PAYG"){ if ($data.sku -like "Enterprise"){ - $total.payg_ent += $data.vcpus + $subtotal.payg_ent += $data.vcpus }elseif ($data.sku -like "Standard"){ - $total.payg_std += $data.vcpus + $subtotal.payg_std += $data.vcpus }elseif ($data.sku -like "Developer"){ - $total.developer += $data.vcpus + $subtotal.developer += $data.vcpus }elseif ($data.sku -like "Express"){ - $total.express += $data.vcpus + $subtotal.express += $data.vcpus } } } } - - $usage += ,(@($sub.Name, $sub.Id, $total.ahb_std, $total.ahb_ent, $total.payg_std, $total.payg_ent, $total.hadr_std, $total.hadr_ent, $total.developer, $total.express)) - + foreach( $property in $total.psobject.properties.name ){ + $total.$property += $subtotal.$property + } + if ($ec -eq $null){ + $usage += ,(@($sub.Name, $sub.Id, $subtotal.ahb_std, $subtotal.ahb_ent, $subtotal.payg_std, $subtotal.payg_ent, $subtotal.hadr_std, $subtotal.hadr_ent, $subtotal.developer, $subtotal.express)) + }else{ + $usage += ,(@($sub.Name, $sub.Id, ($subtotal.ahb_std + $subtotal.ahb_ent*4), ($subtotal.payg_std + $subtotal.payg_ent*4), $subtotal.ahb_std, $subtotal.ahb_ent, $subtotal.payg_std, $subtotal.payg_ent, $subtotal.hadr_std, $subtotal.hadr_ent, $subtotal.developer, $subtotal.express)) + } +} + +if ($ec -eq $null){ + $usage += ,(@("Total", $null, $total.ahb_std, $total.ahb_ent, $total.payg_std, $total.payg_ent, $total.hadr_std, $total.hadr_ent, $total.developer, $total.express)) +}else{ + $usage += ,(@("Total", $null, ($total.ahb_std + $total.ahb_ent*4), ($total.payg_std + $total.payg_ent*4), $total.ahb_std, $total.ahb_ent, $total.payg_std, $total.payg_ent, $total.hadr_std, $total.hadr_ent, $total.developer, $total.express)) } $table = ConvertFrom-Csv ($usage | %{ $_ -join ','} )