Created
September 16, 2019 03:19
-
-
Save jetstreamin/8cbd249264bc6c3cb3d9c763b692887e to your computer and use it in GitHub Desktop.
Get All VM Info for an Azure Subscription
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[string]$tenantId="", | |
[string]$file="Azure-Classic-VMs.csv" | |
) | |
if ($tenantId -eq "") { | |
Write-Host "No tenant specified." | |
Add-AzureAccount | |
} else { | |
add-azureaccount -Tenant $tenantId | |
} | |
$subs = Get-AzureSubscription | |
$vmobjs = @() | |
foreach ($sub in $subs | where-object { ($_.TenantId -eq $tenantId) -or ($tenantId -eq "") } ) | |
{ | |
Write-Host Processing subscription $sub.SubscriptionName | |
try | |
{ | |
Select-AzureSubscription -SubscriptionId $sub.SubscriptionId -ErrorAction Continue | |
$vms = Get-AzureVm | |
$svcs = Get-AzureService | |
foreach ($vm in $vms) | |
{ | |
$service = $svcs | where-object { $_.ServiceName -eq $vm.ServiceName } | |
$vmInfo = [pscustomobject]@{ | |
'Subscription'=$sub.SubscriptionName | |
'Mode'='Classic' | |
'Name'=$vm.Name | |
'ServiceName' = $vm.ServiceName | |
'Location' = $service.Location | |
'VMSize' = $vm.InstanceSize | |
'Status' = $vm.Status | |
'AvailabilitySet' = $vm.AvailabilitySetName} | |
$vmobjs += $vmInfo | |
} | |
} | |
catch | |
{ | |
Write-Host $error[0] | |
} | |
} | |
$vmobjs | Export-Csv -Path $file | |
Write-Host "VM list written to $file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment