-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApplyAHUB.ps1
More file actions
141 lines (127 loc) · 6.44 KB
/
ApplyAHUB.ps1
File metadata and controls
141 lines (127 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#Get all subscriptions
$azSubs = Get-AzSubscription
$dot = "....................."
$AzureVM = @()
$AzureSQLVM = @()
$AzureSQLDB = @()
$AzureSQLMIList = @()
#Iterate through all subscriptions
foreach ($azSub in $azSubs)
{
$string = "Checking subscription: "
$string + $azSub.Name + $dot
Set-AzContext -Subscription $azSub | Out-Null
#Iterate through all VMs----------------------------------------------------
foreach ($azVM in Get-AzVM)
{
#If AHUB is not applied for Windows Server
if ($azVM.StorageProfile.OsDisk.OsType -ceq "Windows")
{
if ((!$azVM.LicenseType) -or ($azVM.LicenseType -ceq "None"))
{
$string = "[UPDATE] Updating VM License with AHUB (Windows Server): "
$string + $azVM.Name + $dot
$azVM.LicenseType = "Windows_Server"
#Apply AHUB
Update-AzVM -ResourceGroupName $azVM.ResourceGroupName -VM $azVM
#Adding details for CSV file
$props = @{
SubName = $azSub.Name
VMName = $azVM.Name
Region = $azVM.Location
OsType = $azVM.StorageProfile.OsDisk.OsType
ResourceGroupName = $azVM.ResourceGroupName
LicenseType = $azVM.LicenseType
}
$ServiceObject = New-Object -TypeName PSObject -Property $props
$AzureVM += $ServiceObject
}
}
}
#Iterate through all SQL Server VMs----------------------------------------------------
foreach ($azSqlVM in Get-AzSqlVM)
{
#Only Enterprise or Standard SKUs are supported for AHUB
if (($azSqlVM.Sku -ceq 'Standard') -or ($azSqlVM.Sku -ceq 'Enterprise'))
{
if ($azSqlVM.LicenseType -ceq "PAYG")
{
$string = "[UPDATE] Updating VM License with AHUB (Microsoft SQL): "
$string + $azSqlVM.Name + $dot
#Updating License Type to AHUB
Update-AzSqlVM -ResourceGroupName $azSqlVM.ResourceGroupName -Name $azSqlVM.Name -LicenseType "AHUB"
# Adding details for CSV file
$propsSQL = @{
SubName = $azSub.Name
VMName = $azSqlVM.Name
Region = $azSqlVM.Location
Sku = $azSqlVM.Sku
ResourceGroupName = $azSqlVM.ResourceGroupName
}
$SQLServiceObject = New-Object -TypeName PSObject -Property $propsSQL
$AzureSQLVM += $SQLServiceObject
}
}
else {
$string = "[WARNING] Az SQL VM SKU for "
$string2 = " does not support AHUB License"
$string + $azSqlVM.Name + $azSqlVM.Sku + $string2
}
}
#Iterate through all SQL Databases ----------------------------------------------------
$AzureSQLServers = Get-AzResource | Where-Object ResourceType -EQ Microsoft.SQL/servers
foreach ($AzureSQLServer in $AzureSQLServers)
{
#Iterate through all SQL Server DBs that are not masters and have vCore-based purchasing model
$AzureSQLServerDatabases = Get-AzSqlDatabase -ServerName $AzureSQLServer.Name -ResourceGroupName $AzureSQLServer.ResourceGroupName | Where-Object DatabaseName -NE "master"
foreach ($AzureSQLDatabase in $AzureSQLServerDatabases)
{
if ($AzureSQLDatabase.LicenseType -cne "BasePrice")
{
$string = "[UPDATE] Updating Azure SQL Database with AHUB: "
$string + $AzureSQLDatabase.DatabaseName + $dot
#Updating License Type to BasePrice (AHB Applied)
Set-AzSqlDatabase -ServerName $AzureSQLServer.Name -ResourceGroupName $AzureSQLServer.ResourceGroupName -DatabaseName $AzureSQLDatabase.DatabaseName -LicenseType "BasePrice"
# Adding details for CSV file
$propsSQL_DB = @{
SubName = $azSub.Name
ServerName = $AzureSQLServer.Name
ResourceGroupName = $AzureSQLServer.ResourceGroupName
DatabaseName = $AzureSQLDatabase.DatabaseName
}
$SQLDBObject = New-Object -TypeName PSObject -Property $propsSQL_DB
$AzureSQLDB += $SQLDBObject
}
}
}
# Iterate through all SQL Managed Instances ----------------------------------------------------
$AzureSQLManagedInstances = Get-AzResource | Where-Object ResourceType -EQ Microsoft.SQL/managedInstances
foreach ($AzureSQLMI in $AzureSQLManagedInstances)
{
if ($AzureSQLMI.LicenseType -cne "BasePrice")
{
$string = "[UPDATE] Updating Azure SQL MI with AHUB: "
$string + $AzureSQLMI.Name + $dot
#Updating License Type to BasePrice (AHB Applied)
Set-AzSqlInstance -Name $AzureSQLMI.Name -ResourceGroupName $AzureSQLMI.ResourceGroupName -LicenseType BasePrice -Force
# Adding details for CSV file
$propsSQL_MI = @{
SubName = $azSub.Name
ServerName = $AzureSQLMI.Name
ResourceGroupName = $AzureSQLMI.ResourceGroupName
}
$SQLMIObject = New-Object -TypeName PSObject -Property $propsSQL_MI
$AzureSQLMIList += $SQLMIObject
}
}
}
$AzureVM | Export-Csv -Path "$($home)\AzVM-WindowsServer-Licensing-Change.csv" -NoTypeInformation -force
$AzureSQLVM | Export-Csv -Path "$($home)\AzVM-SQLVM_Std_Ent-Licensing-Change.csv" -NoTypeInformation -force
$AzureSQLDB | Export-Csv -Path "$($home)\AzSQL-DB-Licensing-Change.csv" -NoTypeInformation -force
$AzureSQLMIList | Export-Csv -Path "$($home)\AzSQLMI-Licensing-Change.csv" -NoTypeInformation -force
echo "------------AHUB has been applied------------"
echo "Changes have been logged as CSV files to the following files:"
echo "AzVM-WindowsServer-Licensing-Change.csv --> Windows Server license type changes......"
echo "AzVM-SQLVM_Std_Ent-Licensing-Change.csv --> SQL Standard/Enterprise license type changes......"
echo "AzSQL-DB-Licensing-Change.csv --> SQL Databases license type changes......"
echo "AzSQLMI-Licensing-Change.csv --> SQL Databases license type changes......"