-
Notifications
You must be signed in to change notification settings - Fork 1
/
List-and-Export-VM-SKUs-in-region-with-minimum-number-of-NICs.ps1
196 lines (143 loc) · 8.92 KB
/
List-and-Export-VM-SKUs-in-region-with-minimum-number-of-NICs.ps1
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<#
.SYNOPSIS
A script used to list and export VM SKUs in a specified region that supports a specified minimum number of NICs.
.DESCRIPTION
A script used to list and export VM SKUs in a specified region that supports a specified minimum number of NICs.
The script will do all of the following:
Remove the breaking change warning messages.
Import the Az.Compute module if it is not already imported.
Validate if the provided region is a valid Azure region, otherwise exit the script.
Create the C:\Temp folder if it does not exist.
List VM SKUs with the specified value or more network interfaces in the specified region.
Export the filtered SKUs to a CSV file without including type information.
Open the CSV file.
.NOTES
Filename: List-and-Export-VM-SKUs-in-region-with-minimum-number-of-NICs.ps1
Created: 23/05/2024
Last modified: 23/05/2024
Author: Wim Matthyssen
Version: 1.0
PowerShell: Azure PowerShell and Azure Cloud Shell
Requires: PowerShell Az (v10.4.1)
Action: Change variables where needed to fit your needs.
Disclaimer: This script is provided "as is" with no warranties.
.EXAMPLE
Connect-AzAccount
Get-AzTenant (if not using the default tenant)
Set-AzContext -tenantID "xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx" (if not using the default tenant)
.\List-and-Export-VM-SKUs-in-region-with-minimum-number-of-NICs.ps1 -region <"Azure region here>" -minNics <"minimum number of NICs here>"
-> .\List-and-Export-VM-SKUs-in-region-with-minimum-number-of-NICs.ps1 -region westeurope -minNics 3
.LINK
https://wmatthyssen.com/2024/05/23/using-an-azure-powershell-script-to-list-and-export-azure-vm-skus-by-region-with-minimum-nics/
#>
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Parameters
param(
# $region -> Name of Azure region
[parameter(Mandatory =$true)][ValidateNotNullOrEmpty()] [string] $region,
# $minNics -> Minimum number of network interfaces
[parameter(Mandatory =$true)] [ValidateNotNullOrEmpty()] [ValidateRange(1,8)] [int] $minNics
)
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Variables
$driveLetter = "C:"
$tempFolderName = "Temp"
$tempFolderPath = Join-Path -Path $driveLetter -ChildPath $tempFolderName
$itemType = "Directory"
$csvFileName = "VM_SKUs.csv"
$global:currenttime= Set-PSBreakpoint -Variable currenttime -Mode Read -Action {$global:currenttime= Get-Date -UFormat "%A %m/%d/%Y %R"}
$foregroundColor1 = "Green"
$foregroundColor2 = "Yellow"
$writeEmptyLine = "`n"
$writeSeperatorSpaces = " - "
# ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Remove the breaking change warning messages
Set-Item -Path Env:\SuppressAzurePowerShellBreakingChangeWarnings -Value $true | Out-Null
Update-AzConfig -DisplayBreakingChangeWarning $false | Out-Null
$warningPreference = "SilentlyContinue"
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Write script started
Write-Host ($writeEmptyLine + "# Script started. Without errors, it can take up to 1 minutes to complete" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Import the Az.Compute module if it is not already imported
if (-not (Get-Module -Name Az.Compute)) {
Import-Module Az.Compute
}
Write-Host ($writeEmptyLine + "# Az.compute module available" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Validate if the provided region is a valid Azure region, otherwise exit the script
# Define a list of valid Azure regions
$validRegions = @(
"eastus", "eastus2", "centralus", "northcentralus", "southcentralus",
"westus", "westus2", "westus3", "canadacentral", "canadaeast",
"brazilsouth", "brazilsoutheast", "northeurope", "westeurope",
"francecentral", "francesouth", "uksouth", "ukwest",
"germanynorth", "germanywestcentral", "norwayeast", "norwaywest",
"switzerlandnorth", "switzerlandwest", "italynorth", "italynorth2",
"eastasia", "southeastasia", "australiaeast", "australiasoutheast",
"australiacentral", "australiacentral2", "chinaeast", "chinanorth",
"chinaeast2", "chinanorth2", "centralindia", "southindia", "westindia",
"japaneast", "japanwest", "koreacentral", "koreasouth",
"southafricanorth", "southafricawest", "uaenorth", "uaecentral",
"israelcentral", "saudiarabiawest", "saudiarabiaeast"
)
# Check if the provided region is valid
if ($region -notin $validRegions) {
Write-Host ($writeEmptyLine + "# The provided region $region is not a valid Azure region. Please provide a valid Azure region." + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor2 $writeEmptyLine
exit
}
Write-Host ($writeEmptyLine + "# Specified region $region is a valid Azure region" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Create the C:\Temp folder if not exists
If(!(Test-Path $tempFolderPath))
{
New-Item -Path $driveLetter -Name $folderName -ItemType $itemType -Force | Out-Null
}
Write-Host ($writeEmptyLine + "# $tempFolderName folder available under the C: drive" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## List VM SKUs with the specified value or more network interfaces in the specified region
# Get all compute resource SKUs
$allSkus = Get-AzComputeResourceSku
# Filter SKUs based on location, resource type, and network interface capabilities
$filteredSkus = $allSkus | Where-Object {
$_.Locations.Contains($region) -and
$_.ResourceType -eq "virtualMachines" -and
$_.Capabilities.Where({
$_.Name -eq "MaxNetworkInterfaces" -and
[int]$_.Value -ge $minNics
})
}
# Create custom objects for each filtered SKU
$customObjects = $filteredSkus | ForEach-Object {
$maxNics = $_.Capabilities |
Where-Object {$_.Name -eq "MaxNetworkInterfaces"} |
Select-Object -ExpandProperty Value
# Structure the data in the CSV file with the required columns Name, Tier, Size, and MaxNetworkInterfaces
[PSCustomObject]@{
'Name' = $_.Name
'Tier' = $_.Tier
'Size' = $_.Size
'MaxNetworkInterfaces' = $maxNics
}
}
# Export the filtered SKUs to a CSV file without including type information
$customObjects | Export-Csv -Path C:\Temp\VM_SKUs.csv -NoTypeInformation
Write-Host ($writeEmptyLine + "# CSV file $csvFileName created" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Open the CSV file
# Combine the directory path and the filename into a full path
$fullPath = Join-Path -Path $tempFolderPath -ChildPath $csvFileName
Invoke-Item $fullPath
Write-Host ($writeEmptyLine + "# CSV file $csvFileName opened" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Write script completed
Write-Host ($writeEmptyLine + "# Script completed" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------