eng-vm-size-collecting.ps1

<#PSScriptInfo
 
.VERSION 1.1
 
.GUID b2ffdb6b-46cb-4214-a0d5-79449f8e36ea
 
.AUTHOR jileeon@gmail.com
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>
 

#Requires -Module AzureRM.Automation
#Requires -Module AzureRM.Profile
#Requires -Module AzureRM.Resources
#Requires -Module AzureRm.Storage
#Requires -Module Azure.Storage
#Requires -Module AzureRM.Compute
#Requires -Module AzureRmStorageTable

<#
 
.DESCRIPTION
 Storing new VM sizes to Azure Table Storage via Azure Automation
.NOTES
 Create an empty table storage and change proper config values in this runbook source code.
 Run & Confirm stored data in the table storage. After the first execution, you can see all VM sizes on the table storage.
 
#>




# Config
# It is required to execute this script and you can change values you want.
$RESOURCE_GROUP_NAME = "eng-vm-size-collector"  # A resource group name of a storage account
$STORAGE_ACCOUNT_NAME = "azurevmsize"  # A storage account name
$STORAGE_TABLE_NAME = "VMSizeList"  # A table name to update VM sizes

# Login by the connection of this automation account.
$conn = Get-AutomationConnection -Name "AzureRunAsConnection"
Add-AzureRmAccount `
    -ServicePrincipal `
    -TenantId $conn.TenantId `
    -ApplicationId $conn.ApplicationId `
    -CertificateThumbprint $conn.CertificateThumbprint | out-null

# Getting latest VM size data
$vm_size_table = Get-AzureStorageTableTable `
                    -resourceGroup $RESOURCE_GROUP_NAME `
                    -tableName $STORAGE_TABLE_NAME `
                    -storageAccountName $STORAGE_ACCOUNT_NAME
$vm_size_list_orig = Get-AzureStorageTableRowAll -table $vm_size_table
$newly_added = New-Object System.Collections.ArrayList

# Getting locations
$resources = Get-AzureRmResourceProvider -ProviderNamespace Microsoft.Compute
$location_list = $resources.ResourceTypes.Where{($_.ResourceTypeName -eq 'virtualMachines')}.Locations

# Adding new VM size
foreach ($location in $location_list) {
    $vm_size_list_by_region = Get-AzureRmVmSize -Location $location
    foreach($vm_size in $vm_size_list_by_region) {
        if (
            (($vm_size_list_orig | where-object {$_.Name -eq $vm_size.Name}) -eq $null) -and
            (($newly_added | where-object {$_.Name -eq $vm_size.Name}) -eq $null)
        ) {
            Add-StorageTableRow `
                -table $vm_size_table `
                -partitionKey "VMSize" `
                -rowKey $vm_size.Name `
                -property @{
                    "Name"=$vm_size.Name;
                    "NumberOfCores"=$vm_size.NumberOfCores;
                    "OSDiskSizeInMB"=$vm_size.OSDiskSizeInMB;
                    "ResourceDiskSizeInMB"=$vm_size.ResourceDiskSizeInMB;
                    "MemoryInMB"=$vm_size.MemoryInMB;
                    "MaxDataDiskCount"=$vm_size.MaxDataDiskCount
                } | out-null
            $newly_added.add($vm_size) | out-null
        }
    }
}