我有这样的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中使用它。
如果要列出特定订阅中的所有VM,则只需要这样:
$subscriptionId = "xxxxx-xxxxx-xxxxx-xxxx"
Select-AzureRMSubscription -Subscription $subscriptionId
Get-AzureRmVM