ForEach-Object在powerhell(Azure VM)中返回所有对象3次

问题描述 投票:0回答:1

我有这样的Powershell行,它应该列出所选订阅中的所有虚拟机,并且我总共有3个订阅。

$azureSubscriptionID = "xxxxx-xxxxx-xxxxx-xxxx"
foreach ($subs in $azureSubscriptionID)
    {
        Write-Output "Collecting VM facts from subscription $subs"

         $vms += Get-AzureRMSubscription | ForEach-Object {Select-AzureRMSubscription $_ | Out-Null; Get-AzureRmVM -WarningAction SilentlyContinue} 
    }

问题是,当运行脚本并使用$ vms时,我将在预订中列出三次所有可用的vms,如下所示:VM A,VM B,虚拟机CVM A,VM B,虚拟机CVM A,VM B,VM C

我在做什么错,以及如何解决?还是有几种方法可以在几行中从X订阅中获取所有VM?在Azure Runbook中使用它。

azure powershell foreach virtual-machine
1个回答
0
投票

如果要列出特定订阅中的所有VM,则只需要这样:

$subscriptionId = "xxxxx-xxxxx-xxxxx-xxxx"

Select-AzureRMSubscription -Subscription $subscriptionId

Get-AzureRmVM
© www.soinside.com 2019 - 2024. All rights reserved.