Skip to content

Instantly share code, notes, and snippets.

@theonlyway
Last active January 27, 2019 05:01
Show Gist options
  • Save theonlyway/83bd738c0f9b0f04b97489f549310e6d to your computer and use it in GitHub Desktop.
Save theonlyway/83bd738c0f9b0f04b97489f549310e6d to your computer and use it in GitHub Desktop.
# Installs the AzureRM PowerShell module from the PSGallery
Install-Module AzureRM -force
# Logs you in to Azure
Login-AzureRmAccount
# Gets your Azure subscription then selects it which essentially logs you in to it. Needs to be done if your account has access
# to multiple subscription
Get-AzureRmSubscription -SubscriptionName "enter your subscription name" | Select-AzureRmSubscription
# Gets a list of all the Azure VM's in your subscription
$vms = Get-AzureRmVM
# Initializes a empty variable for $details to give it the ability to add more than one item easily
$details = @()
# Foreach loop for all of the VM's in $vms
foreach ($vm in $vms)
{
# Get's a list of all of the available instance sizes for the particular VM but filters it out to match the one the VM is currently using
# to get CPU and Memory specs
$vmsize = Get-AzureRmVMSize -VMName $vm.Name -ResourceGroupName $vm.ResourceGroupName | ? {$_.Name -eq $vm.HardwareProfile.VmSize}
# Creates a custom PowerShell object to format the information and take values from both the $vm variable and $vmsize variable
# and for each one of the VM's in the $vms loop it will append it to $details
$details += New-Object -TypeName PSObject -Property @{
ResourceGroup = $vm.ResourceGroupName
Name = $vm.Name
Location = $vm.Location
VMSize = $vmsize.Name
CPU = $vmsize.NumberOfCores
Memory = $vmsize.MemoryInMB
OsType = $vm.StorageProfile.ImageReference.Sku
}
}
# Outputs all of $details to a grid window. Did this for the example but this can be a CSV or JSON file etc
$details | Out-GridView
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment