4

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.

G Beach
  • 115
  • 1
  • 3
  • 10

3 Answers3

3

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" 
Joy Wang
  • 39,905
  • 3
  • 30
  • 54
  • Thanks for this Joy...this certainly gets the instances for one subscription. – G Beach Nov 25 '19 at 10:10
  • Do you know what changes to the code would need to be made in order to get the instances for multiple subscriptions in a tenant, without having to run the above script against each one? – G Beach Nov 25 '19 at 10:11
2

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

PS > Get-AzureRmVM -ResourceGroupName "vmss" -VMScaleSetName "vmss"
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

You can use Get-AzVmss (leverages the Az library) to get scale set info.

PS Get-AzVmss 
Mike Oryszak
  • 298
  • 2
  • 10