[英]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.