简体   繁体   中英

How to export VMData from azure across multiple subscriptions

I have been able to export VMData from a single azure subscription using the code below.

$VMs = Get-AzVM
$vmOutput = $VMs | ForEach-Object { 
    [PSCustomObject]@{
        "VM Name" = $_.Name
    "VM Profile" = $_.HardwareProfile.VmSize
        "VM Type" = $_.StorageProfile.osDisk.osType
        "VM Location" = $_.Location
        "VM Resource Group Name" = $_.ResourceGroupName -join ','
    }
}
$vmOutput | export-csv C:\Azure\VMdata.csv -delimiter ";" -force -notypeinformation

However, when using the code below to try and extract from across multiple subscriptions and get no results.

$azuresubs = Get-AzSubscription
$azureSubs.foreach{
    Select-AzSubscription $_
$VMs = Get-AzVM
$vmOutput = $VMs | ForEach-Object { 
    [PSCustomObject]@{
        "VM Name" = $_.Name
    "VM Profile" = $_.HardwareProfile.VmSize
        "VM Type" = $_.StorageProfile.osDisk.osType
        "VM Location" = $_.Location
        "VM Resource Group Name" = $_.ResourceGroupName -join ','
        }
    }
}
$vmOutput | export-csv C:\Azure\VMdata.csv -delimiter ";" -force -notypeinformation

Being new to powershell, I'm sure the syntax above is incorrect so any assistance would be much appreciated.

Thank you

i think what happens here is you overwrite your variable on each pass (and last pass happens to have no vms). so you need to append to it, not overwrite it. define it before loop:

$vmOutput = @()

and then append to it inside the loop

$vmOutput += $Vms | bla-bla-bla

final:

$vmOutput = @()
$azuresubs = Get-AzSubscription
$azureSubs.foreach{
    Select-AzSubscription $_
    $VMs = Get-AzVM
    $vmOutput += $VMs | ForEach-Object { 
    [PSCustomObject]@{
        xxx
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM