1+ # This scripts calculates the AHB usage by all the SQL resources in a specific subscription
2+ # based on the selected Azure Hybrid Benefit option
3+
4+ # Set the subscription Id
5+ $SubcriptionId = read-host - Prompt " Enter Subscription ID"
6+ Set-AzContext - SubscriptionId $SubcriptionId
7+
8+ # Variables to keep track of SQL VMs and VCPUs count
9+ $total_std_vcores = 0
10+ $total_ent_vcores = 0
11+
12+ # Get all SQL databadses in the subscription
13+ $databases = Get-AzSqlServer | Get-AzSqlDatabase
14+
15+ # Get the databases with License Included and add to VCore count
16+ foreach ($db in $databases ){
17+ if (($db.SkuName -ne " ElasticPool" ) -and ($db.LicenseType -eq " LicenseIncluded" )) {
18+ if ($db.Edition -eq " BusinessCritical" ) {
19+ $total_ent_vcores += $db.Capacity
20+ } elseif ($db.Edition -eq " GeneralPurpose" ) {
21+ $total_std_vcores += $db.Capacity
22+ }
23+ }
24+ }
25+
26+ # Get all SQL elastic pools in the subscription
27+ $pools = Get-AzSqlServer | Get-AzSqlElasticPool
28+
29+ # Get the elastic pools with License Included and and add to VCore count
30+ foreach ($pool in $pools ){
31+ if ($pool.LicenseType -eq " LicenseIncluded" ) {
32+ if ($pool.Edition -eq " BusinessCritical" ) {
33+ $total_ent_vcores += $pool.Capacity
34+ } elseif ($pool.Edition -eq " GeneralPurpose" ) {
35+ $total_std_vcores += $pool.Capacity
36+ }
37+ }
38+ }
39+
40+ # Get all SQL managed instances in the subscription
41+ $instances = Get-AzSqlInstance
42+
43+ # Get the SQL managed instances with License Included and add to VCore count
44+ foreach ($ins in $instances ){
45+ if (($ins.InstancePoolName -eq $null ) -and ($ins.LicenseType -eq " LicenseIncluded" )) {
46+ if ($ins.Sku.Tier -eq " BusinessCritical" ) {
47+ $total_ent_vcores += $ins.VCores
48+ } elseif ($ins.Sku.Tier -eq " GeneralPurpose" ) {
49+ $total_std_vcores += $ins.VCores
50+ }
51+ }
52+ }
53+
54+ # Get all instance pools in the subscription
55+ $ipools = Get-AzSqlInstancePool
56+
57+ # Get the instance pools with License Included and add to VCore count
58+ foreach ($ip in $ipools ){
59+ if ($ip.LicenseType -eq " LicenseIncluded" ) {
60+ if ($ip.Edition -eq " BusinessCritical" ) {
61+ $total_ent_vcores += $ip.VCores
62+ } elseif ($ip.Edition -eq " GeneralPurpose" ) {
63+ $total_std_vcores += $ip.VCores
64+ }
65+ }
66+ }
67+
68+ # Get All Sql VMs with AHB license configured
69+ $sql_vms = Get-AzSqlVM | where {$_.LicenseType.Contains (" AHUB" )}
70+
71+ # Get the VM size, match it with the corresponding VCPU count and add to VCore count
72+ foreach ($sql_vm in $sql_vms ){
73+ $vm = Get-AzVm - Name $sql_vm.Name - ResourceGroupName $sql_vm.ResourceGroupName
74+ $vm_size = $vm.HardwareProfile.VmSize
75+ # Select first size and get the VCPus available
76+ $size_info = Get-AzComputeResourceSku | where {$_.ResourceType.Contains (' virtualMachines' ) -and $_.Name -like $vm_size } | Select-Object - First 1
77+ # Save the VCPU count
78+ $vcpu = $size_info.Capabilities | Where-Object {$_.name -eq " vCPUsAvailable" }
79+
80+ if ($vcpu ){
81+ $data = [pscustomobject ]@ {vm_resource_uri = $vm.Id ;sku = $sql_vm.Sku ;size = $vm_size ;vcpus = $vcpu.value }
82+ $array += $data
83+
84+ if ($data.sku -like " Enterprise" ){
85+ $total_ent_vcores += $data.vcpus
86+ }elseif ($data.sku -like " Standard" ){
87+ $total_std_vcores += $data.vcpus
88+ }
89+ }
90+ }
91+
92+ Write-Host " Total number of VCores for SQL Enterprise: " $total_ent_vcores
93+ Write-Host " Total number of VCores for SQL Standard: " $total_std_vcores
0 commit comments