3
votes

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.

3

3 Answers

1
votes

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" 
2
votes

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

PS > Get-AzureRmVM -ResourceGroupName "vmss" -VMScaleSetName "vmss"
0
votes

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

PS Get-AzVmss