Azure VMs issue

We have a lot of Azure subscriptions, and we plan to grab the details of all Azure VMs and generate csv files via the az module. I implemented this functionality through a script. But I found that this script generates a csv per subscription, is there a way to integrate it into all subscriptions in one csv

The specific script is as follows

$subid = Get-AzSubscription

foreach ($sub in $subid) {
    Select-AzSubscription -SubscriptionId $sub.Id
    $subname = $sub.Name
    $vms = Get-AzVM -Status
    $nics = Get-AzNetworkInterface
    $pips = Get-AzPublicIpAddress

   function GetResourceNameFromResourceId($resourceId) {
        return $resourceId.Substring($resourceId.LastIndexOf('/') + 1);
    }
    
    $vmss = foreach ($vm in $vms) {
        $nicResourceId = $vm.NetworkProfile.NetworkInterfaces[0].Id
        $nicInterfaces = GetResourceNameFromResourceId($nicResourceId)
        $nic = $nics | Where-Object { $_.Name -eq $nicInterfaces -and $_.ResourceGroupName -eq $vm.ResourceGroupName }
        $ipResourceId = $nic.IpConfigurations[0].PublicIpAddress.Id
        $ipAddress = GetResourceNameFromResourceId($ipResourceId)
        $pip = $pips | Where-Object { $_.Name -eq $ipAddress -and $_.ResourceGroupName -eq $vm.ResourceGroupName }
        $availabilitysetname = ($vm.AvailabilitySetReference.Id -split "/")[-1]
        $props = @{
            "vmname"                    = $vm.name;
            "area"                      = $vm.Location;
            "PowerState"                = $vm.PowerState;
            "ResourceGroupName"         = $vm.ResourceGroupName;
            "availabilitysetname"       = $availabilitysetname;
            "PrivateIpAddress"          = $nic.IpConfigurations[0].PrivateIpAddress;
            "PrivateIpAllocationMethod" = $nic.IpConfigurations[0].PrivateIpAllocationMethod;
            "VmSize"                    = $vm.HardwareProfile.VmSize;
            "system"                    = $vm.StorageProfile.OsDisk.OsType.ToString();
            "publicIP"                  = $pip.IpAddress;
            "PublicIpAllocationMethod"  = $pip.PublicIpAllocationMethod;
        }
        New-Object -TypeName PSObject -Property $props
    }
    $vmss | Select-Object * | Export-Csv -Path .\$subname.csv
}

Funny - the way you ask sounds like you’re surprised by your own code!? :thinking: :smirk: … the code is meant to act exactly like this - output the results per subscription … :man_shrugging:t4:

Move the variable assignment for the resulting variable you use to output the results outside your outer most loop and output it after your run the outer most loop. … easy, hu? :wink: :man_shrugging:t4: