Skip to content

Commit 0b446ff

Browse files
authored
Merge pull request #858 from anosov1960/master
Added EC calculation
2 parents 24b19c6 + 5fd497d commit 0b446ff

File tree

3 files changed

+61
-44
lines changed

3 files changed

+61
-44
lines changed

samples/manage/azure-hybrid-benefit/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.date: 12/15/2020
88

99
# Overview
1010

11-
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:
11+
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:
1212

1313
- AHB Standard vCores
1414
- AHB Enterprise vCores
@@ -58,6 +58,6 @@ If you need to scan a subset of the subscriptions, use the following steps:
5858

5959
> [!NOTE]
6060
> - To paste the commands into the shell, use `Ctrl-Shift-V` on Windows or `Cmd-v` on MacOS.
61-
> - The script will be uploaded directly to the home folder associated with your Cloud Shell session.
61+
> - The `curl` command will copy the script directly to the home folder associated with your Cloud Shell session.
6262
> - The script will prompt for the resource group name and print a message when migration is completed.
6363

samples/manage/azure-hybrid-benefit/ahb-usage-in-subscription.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Set-AzContext -SubscriptionId $SubcriptionId
2828
$total_std_vcores = 0
2929
$total_ent_vcores = 0
3030

31-
#Get all SQL databadses in the subscription
31+
#Get all SQL databases in the subscription
3232
$databases = Get-AzSqlServer | Get-AzSqlDatabase
3333

3434
# Get the databases with License Included and add to VCore count

samples/manage/azure-hybrid-benefit/sql-license-usage.ps1

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ if ($args[0] -ne $null) {
4141
}
4242

4343
[System.Collections.ArrayList]$usage = @()
44-
$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"))
44+
if ($ec -eq $null){
45+
$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"))
46+
}else{
47+
$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"))
48+
}
49+
$total = [pscustomobject]@{ahb_std=0; ahb_ent=0; payg_std=0; payg_ent=0; hadr_std=0; hadr_ent=0; developer=0; express=0}
50+
4551

4652
#Save the VM SKU table
4753
$VM_SKUs = Get-AzComputeResourceSku
@@ -61,70 +67,70 @@ foreach ($sub in $subscriptions){
6167
}
6268

6369
# Total counts of different license types
64-
$total = [pscustomobject]@{ahb_std=0; ahb_ent=0; payg_std=0; payg_ent=0; hadr_std=0; hadr_ent=0; developer=0; express=0}
70+
$subtotal = [pscustomobject]@{ahb_std=0; ahb_ent=0; payg_std=0; payg_ent=0; hadr_std=0; hadr_ent=0; developer=0; express=0}
6571

6672
#Get all logical servers
6773
$servers = Get-AzSqlServer
6874

69-
#Get all SQL databadse resources in the subscription
75+
#Get all SQL database resources in the subscription
7076
$databases = $servers | Get-AzSqlDatabase
7177

72-
# Get the databases with License Included and add to VCore count
78+
# Process the vCore-based databases
7379
foreach ($db in $databases ){
7480
if ($db.SkuName -eq "ElasticPool") {continue}
7581

7682
if ($db.LicenseType -eq "LicenseIncluded") {
7783
if ($db.Edition -eq "BusinessCritical") {
78-
$total.ahb_ent += $db.Capacity
84+
$subtotal.ahb_ent += $db.Capacity
7985
} elseif ($db.Edition -eq "GeneralPurpose") {
80-
$total.ahb_std += $db.Capacity
86+
$subtotal.ahb_std += $db.Capacity
8187
}
8288
}else{
8389
if ($db.Edition -eq "BusinessCritical") {
84-
$total.payg_ent += $db.Capacity
90+
$subtotal.payg_ent += $db.Capacity
8591
} elseif ($db.Edition -eq "GeneralPurpose") {
86-
$total.payg_std += $db.Capacity
87-
}$table
92+
$subtotal.payg_std += $db.Capacity
93+
}
8894
}
8995
}
9096

9197
#Get all SQL elastic pool resources in the subscription
9298
$pools = $servers | Get-AzSqlElasticPool
9399

94-
# Get the elastic pools with License Included and and add to VCore count
100+
# Process the vCore-based elastic pools
95101
foreach ($pool in $pools){
96102
if ($pool.LicenseType -eq "LicenseIncluded") {
97103
if ($pool.Edition -eq "BusinessCritical") {
98-
$total.ahb_ent += $pool.Capacity
104+
$subtotal.ahb_ent += $pool.Capacity
99105
} elseif ($pool.Edition -eq "GeneralPurpose") {
100-
$total.ahb_std += $pool.Capacity
106+
$subtotal.ahb_std += $pool.Capacity
101107
}
102108
}else{
103109
if ($pool.Edition -eq "BusinessCritical") {
104-
$total.payg_ent += $pool.Capacity
110+
$subtotal.payg_ent += $pool.Capacity
105111
} elseif ($pool.Edition -eq "GeneralPurpose") {
106-
$total.payg_std += $pool.Capacity
112+
$subtotal.payg_std += $pool.Capacity
107113
}
108114
}
109115
}
110116

111117
#Get all SQL managed instance resources in the subscription
112118
$instances = Get-AzSqlInstance
113119

114-
# Get the SQL managed instances with License Included and add to VCore count
120+
# Process the SQL managed instances with License Included and add to VCore count
115121
foreach ($ins in $instances){
116122
if ($ins.InstancePoolName -eq $null){
117123
if ($ins.LicenseType -eq "LicenseIncluded") {
118124
if ($ins.Sku.Tier -eq "BusinessCritical") {
119-
$total.ahb_ent += $ins.VCores
125+
$subtotal.ahb_ent += $ins.VCores
120126
} elseif ($ins.Sku.Tier -eq "GeneralPurpose") {
121-
$total.ahb_std += $ins.VCores
127+
$subtotal.ahb_std += $ins.VCores
122128
}
123129
}else{
124130
if ($ins.Edition -eq "BusinessCritical") {
125-
$total.payg_ent += $pool.Capacity
131+
$subtotal.payg_ent += $pool.Capacity
126132
} elseif ($ins.Edition -eq "GeneralPurpose") {
127-
$total.payg_std += $ins.Capacity
133+
$subtotal.payg_std += $ins.Capacity
128134
}
129135
}
130136
}
@@ -133,19 +139,19 @@ foreach ($sub in $subscriptions){
133139
#Get all instance pool resources in the subscription
134140
$ipools = Get-AzSqlInstancePool
135141

136-
# Get the instance pools with License Included and add to VCore count
142+
# Process the instance pools
137143
foreach ($ip in $ipools){
138144
if ($ip.LicenseType -eq "LicenseIncluded") {
139145
if ($ip.Edition -eq "BusinessCritical") {
140-
$total.ahb_ent += $ip.VCores
146+
$subtotal.ahb_ent += $ip.VCores
141147
} elseif ($ip.Edition -eq "GeneralPurpose") {
142-
$total.ahb_std += $ip.VCores
148+
$subtotal.ahb_std += $ip.VCores
143149
}
144150
}else{
145151
if ($ip.Edition -eq "BusinessCritical") {
146-
$total.payg_ent += $ip.Capacity
152+
$subtotal.payg_ent += $ip.Capacity
147153
} elseif ($ip.Edition -eq "GeneralPurpose") {
148-
$total.payg_std += $ip.Capacity
154+
$subtotal.payg_std += $ip.Capacity
149155
}
150156
}
151157
}
@@ -165,19 +171,19 @@ foreach ($sub in $subscriptions){
165171
if ($ssis_ir.State -eq "Started"){
166172
if ($ssis_ir.LicenseType -like "LicenseIncluded"){
167173
if ($ssis_ir.Edition -like "Enterprise"){
168-
$total.ahb_ent += $vcpu.value
174+
$subtotal.ahb_ent += $vcpu.value
169175
}elseif ($ssis_ir.Edition -like "Standard"){
170-
$total.ahb_std += $vcpu.value
176+
$subtotal.ahb_std += $vcpu.value
171177
}
172178
}elseif ($data.license -like "BasePrice"){
173179
if ($ssis_ir.Edition -like "Enterprise"){
174-
$total.payg_ent += $vcpu.value
180+
$subtotal.payg_ent += $vcpu.value
175181
}elseif ($ssis_ir.Edition -like "Standard"){
176-
$total.payg_std += $vcpu.value
182+
$subtotal.payg_std += $vcpu.value
177183
}elseif ($ssis_ir.Edition -like "Developer"){
178-
$total.developer += $vcpu.value
184+
$subtotal.developer += $vcpu.value
179185
}elseif ($ssis_ir.Edition -like "Express"){
180-
$total.express += $vcpu.value
186+
$subtotal.express += $vcpu.value
181187
}
182188
}
183189
}
@@ -201,33 +207,44 @@ foreach ($sub in $subscriptions){
201207

202208
if ($data.license -like "DR"){
203209
if ($data.sku -like "Enterprise"){
204-
$total.hadr_ent += $data.vcpus
210+
$subtotal.hadr_ent += $data.vcpus
205211
}elseif ($data.sku -like "Standard"){
206-
$total.hadr_std += $data.vcpus
212+
$subtotal.hadr_std += $data.vcpus
207213
}
208214
}elseif ($data.license -like "AHUB"){
209215
if ($data.sku -like "Enterprise"){
210-
$total.ahb_ent += $data.vcpus
216+
$subtotal.ahb_ent += $data.vcpus
211217
}elseif ($data.sku -like "Standard"){
212-
$total.ahb_std += $data.vcpus
218+
$subtotal.ahb_std += $data.vcpus
213219
}
214220
}elseif ($data.license -like "PAYG"){
215221
if ($data.sku -like "Enterprise"){
216-
$total.payg_ent += $data.vcpus
222+
$subtotal.payg_ent += $data.vcpus
217223
}elseif ($data.sku -like "Standard"){
218-
$total.payg_std += $data.vcpus
224+
$subtotal.payg_std += $data.vcpus
219225
}elseif ($data.sku -like "Developer"){
220-
$total.developer += $data.vcpus
226+
$subtotal.developer += $data.vcpus
221227
}elseif ($data.sku -like "Express"){
222-
$total.express += $data.vcpus
228+
$subtotal.express += $data.vcpus
223229
}
224230
}
225231
}
226232
}
227233

228-
229-
$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))
230-
234+
foreach( $property in $total.psobject.properties.name ){
235+
$total.$property += $subtotal.$property
236+
}
237+
if ($ec -eq $null){
238+
$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))
239+
}else{
240+
$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))
241+
}
242+
}
243+
244+
if ($ec -eq $null){
245+
$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))
246+
}else{
247+
$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))
231248
}
232249

233250
$table = ConvertFrom-Csv ($usage | %{ $_ -join ','} )

0 commit comments

Comments
 (0)