Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a list of Azure Virtual Machine Scale Set Instances using powershell

I am trying to get a list of all Virtual Machine Instances within all Scale Sets of a subscription using powershell.

I have been able to list out all the Scalesets by using the code below, but I would like to show all the Virtual Machine instances within each one.

$azureSubs = Get-AzSubscription -TenantID xxxxxxxxxxxxxxxxx

$azureSubs | ForEach-Object {Select-AzSubscription $_ | Out-Null; Get-AzVMss -WarningAction SilentlyContinue} | Export-Csv -Path "c:\Azure\VirtualMachinesScaleSet.csv" -NoTypeInformation

Can anyone suggest anything to help.

like image 635
G Beach Avatar asked Sep 06 '25 03:09

G Beach


2 Answers

You could use the Get-AzVmssVM command, try the script below in each subscription.

$vmss = Get-AzVmss
$instances = foreach($item in $vmss){
    Get-AzVmssVM -ResourceGroupName $item.ResourceGroupName -VMScaleSetName $item.Name
}
$instances | Export-Csv -Path "C:\Users\joyw\Desktop\ins.csv" 

enter image description here

Update:

For multiple subscriptions in a tenant,try the script below.

$subs = Get-AzSubscription -TenantId "<tenant-id>"
$instances = @()
foreach($sub in $subs){
    Set-AzContext -SubscriptionId $sub.Id
    $vmss = Get-AzVmss
    foreach($item in $vmss){
        $vms = Get-AzVmssVM -ResourceGroupName $item.ResourceGroupName -VMScaleSetName $item.Name
        $instances += $vms
    }
}
$instances | Export-Csv -Path "C:\Users\Administrator\Desktop\ins.csv" 
like image 52
Joy Wang Avatar answered Sep 07 '25 21:09

Joy Wang


You can use Get-AzureRmVM to get the hostname and instance id:

PS > Get-AzureRmVM -ResourceGroupName "vmss" -VMScaleSetName "vmss"
like image 35
Sajeetharan Avatar answered Sep 07 '25 19:09

Sajeetharan