Skip to content

Instantly share code, notes, and snippets.

@MaxMelcher
Created March 27, 2020 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaxMelcher/41d9e4623a54a1acf16bb3f6f6eac73d to your computer and use it in GitHub Desktop.
Save MaxMelcher/41d9e4623a54a1acf16bb3f6f6eac73d to your computer and use it in GitHub Desktop.
VMs, Databases, Storage
<#
.SYNOPSIS
List all Azure VMs across all subscriptions
.DESCRIPTION
List all Azure VMs across all subscriptions
.NOTES
File Name : get-all-vms.ps1
Author : Max Melcher (max.melcher@microsoft.com)
Prerequisite : PowerShell with Azure module
License : Copyright 2020 Max Melcher
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
.EXAMPLE
get-all-vms.ps1 # All VMs in all subscriptions
#>
# the azure module is required
# https://docs.microsoft.com/en-us/powershell/azure/install-az-ps?view=azps-3.6.1
Import-Module Azure
# hide warning output
$WarningPreference = "SilentlyContinue"
# login to azure - use an account that has access to all subscriptions in scope
# Login-AzureRmAccount
$subscriptions = Get-AzureRmSubscription
# the result collections
$resultvm = @()
$resultdb = @()
$resultstorage = @()
foreach ($subscription in $subscriptions) {
Set-AzureRmContext -SubscriptionId $subscription.Id
$vms = Get-AzurermVM
$dbservers = Get-AzureRmSqlServer
$storages = Get-AzureRmStorageAccount
foreach ($vm in $vms) {
# get the status of each vm
$vmstatus = Get-AzurermVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Status
$os = ""
if ($vm.OSProfile.LinuxConfiguration) {
$os = "Linux"
}
else {
$os = "Windows"
}
# add it to the result array
$resultvm += New-Object PSObject -Property @{
Subscription = $subscription.Id
Name = $vm.Name;
OS = $os
PowerState = (get-culture).TextInfo.ToTitleCase(($vmstatus.statuses)[1].code.split("/")[1]);
Size = $vm.HardwareProfile.VmSize
}
}
foreach ($dbserver in $dbservers) {
$dbs = Get-AzureRmSqlDatabase -ResourceGroupName $dbserver.ResourceGroupName -ServerName $dbserver.ServerName
foreach ($db in $dbs) {
$resultdb += New-Object PSObject -Property @{
Subscription = $subscription.Id
Name = $db.DatabaseName
Server = $db.ServerName
}
}
}
foreach ($storage in $storages) {
#get the average capacity of the last 30 days
$usedCapacityInGB = (Get-AzureRmMetric -ResourceId $storage.Id -TimeGrain 30.00:00:00 -MetricName "UsedCapacity").Data.Average / 1024 / 1024 / 1024
# add it to the result array
$resultstorage += New-Object PSObject -Property @{
Subscription = $subscription.Id
Name = $storage.StorageAccountName;
GB = $usedCapacityInGB
}
}
}
write-host "VMs:"
$resultvm | format-table
write-host "DBs:"
$resultdb | format-table
write-host "Storage:"
$resultstorage | format-table
$running = $resultvm | ? { $_.PowerState -ne "Deallocated" } | measure-object
write-host "Running VMs: $($running.count)"
$linux = $resultvm | ? { $_.OS -eq "Linux" -and $_.PowerState -ne "Deallocated" } | measure-object
write-host "Running Linux VMs : $($linux.count)"
$windows = $resultvm | ? { $_.OS -eq "Windows" -and $_.PowerState -ne "Deallocated" } | measure-object
write-host "Running Windows VMs: $($windows.count)"
write-host "Databases: $($resultdb.Count)"
$size = 0
$resultstorage | foreach { $size += $_.GB }
write-host "Storage: $size GB"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment