简体   繁体   English

如何从多个订阅的Azure中导出VMData

[英]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. 我已经可以使用以下代码从单个Azure订阅中导出VMData。

$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. 作为Powershell的新手,我敢肯定上面的语法是不正确的,因此我们将不胜感激。

Thank you 谢谢

i think what happens here is you overwrite your variable on each pass (and last pass happens to have no vms). 我认为这里发生的事情是您在每次通过时都覆盖了变量(而最后一次通过恰好没有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
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从多个ResourceGroup以及多个订阅中导出LocalNetworkGateway信息 - How to export LocalNetworkGateway info from multiple ResourceGroups but also across multiple subscriptions Microsoft Azure:如何在跨多个订阅循环访问资源的脚本中更改订阅 - Microsoft Azure: How to change subscriptions in a script that iterates through resources across multiple subscriptions 如何从多个Azure订阅中导出虚拟机名称和IP地址 - How to export Virtual Machine Names and IP Addresses from within multiple azure subscriptions Azure - 信用分布在多个订阅中 - Azure - Credit spread across multiple subscriptions Azure Powershell - 跨 EA 中的多个订阅 - Azure Powershell - across MULTIPLE subscriptions in an EA Azure 服务连接无法跨多个订阅工作 - Azure Service Connection not working across multiple subscriptions Azure功能的托管身份可以跨多个订阅进行访问吗? - Can Managed Identity of a Azure Function have access across multiple subscriptions? 如何跨多个订阅加密所有Azure存储帐户中的现有数据 - How can I encrypt existing data in ALL Azure storage accounts across multiple subscriptions Azure PowerShell - 从所有订阅中获取 VM 使用情况 - Azure PowerShell - get VM usage from across all subscriptions 从所有订阅的恢复保险库获取所有 Azure 备份 - Getting all Azure backup from recovery vault across all subscriptions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM