如何在 Powershell 中获取所有 Azure 虚拟机的列表
How to get list of all Azure VMs in Powershell
我正在尝试在 Powershell 中获取我所有 Azure VM 的列表。
Get-AzureVM
遗憾的是,这仅 returns 虚拟机(经典).
下列出的 VM
如何获取新虚拟机的列表?
您需要使用 Azure 资源管理器模式访问新 VM:
Switch-AzureMode -Name AzureResourceManager
您可以阅读更多相关信息 here。
根据 David 的回答,我编写了以下结合了两个 VM 列表的脚本:
Switch-AzureMode -Name AzureServiceManagement
#ResourceGroupName will be blank for these
$classicVms = Get-AzureVM | select Name, ServiceName, ResourceGroupName
Switch-AzureMode -Name AzureResourceManager
#ServiceName will be blank for these
$armVms = Get-AzureVM | select Name, ServiceName, ResourceGroupName
$allVms = $classicVms + $armVms
$allVms
当您 运行 执行此操作时,您会收到一条警告,提示 Switch-AzureMode 已弃用。
WARNING: The Switch-AzureMode cmdlet is deprecated and will be removed in a future release
弃用是重大更改的一部分。您可以在此处阅读详细信息:Deprecation of Switch-AzureMode.
请注意 Switch-AzureMode 现已弃用 (https://github.com/Azure/azure-powershell/wiki/Deprecation-of-Switch-AzureMode-in-Azure-PowerShell)。 Cmdlet 重命名 Azure 资源管理模块下的所有 cmdlet 都将重命名以符合以下格式:[Verb]-AzureRm[Noun]
示例:New-AzureVm 变为 New-AzureRmVm
使用Azure CLI,我们可以使用az vm list
命令获取当前订阅中所有虚拟机的列表。除此之外,我们只是遍历所有订阅并将结果添加到一个列表中
$results=New-Object -TypeName System.Collections.ArrayList;
$subs = az account list | ConvertFrom-Json;
$subIndex = 0;
foreach ($sub in $subs)
{
$subIndex++;
Write-Progress "Gathering VMs" -Status $sub.name -PercentComplete ($subIndex / $subs.Count * 100)
$vms = az vm list --subscription $sub.id | ConvertFrom-Json
$results.Add(@{
subscription = $sub
vms = $vms
}) > $null;
$results | ConvertTo-Json -Depth 100 > vms.json;
}
然而,does not include the power on/off state of the vms
我们可以使用 az graph query
命令获取所有 VM 信息 + 电源状态。这有更快的好处。需要做一些工作来处理分页结果,但它仍然相当容易。
$query = "
Resources
| where type == 'microsoft.compute/virtualmachines'
| extend PowerState = tostring(properties.extended.instanceView.powerState.code)
| extend vmSize = tostring(properties.hardwareProfile.vmSize)
";
$params = @(
"--graph-query", $query -replace "`n", ""
"--output", "json"
);
$total=New-Object -TypeName System.Collections.ArrayList;
$count=0;
while ($true)
{
$results = az graph query @params | ConvertFrom-Json;
foreach ($d in $results.data)
{
$total.Add($d);
}
$count += $results.count;
$params = @(
"--graph-query", $query -replace "`n", ""
"--output", "json"
"--skip-token", $results.skip_token
"--skip", $count
);
if ([string]::IsNullOrEmpty($results.skip_token)) {break;}
}
$total | ConvertTo-Json -Depth 100 > vms_fast.json;
我正在尝试在 Powershell 中获取我所有 Azure VM 的列表。
Get-AzureVM
遗憾的是,这仅 returns 虚拟机(经典).
下列出的 VM如何获取新虚拟机的列表?
您需要使用 Azure 资源管理器模式访问新 VM:
Switch-AzureMode -Name AzureResourceManager
您可以阅读更多相关信息 here。
根据 David 的回答,我编写了以下结合了两个 VM 列表的脚本:
Switch-AzureMode -Name AzureServiceManagement
#ResourceGroupName will be blank for these
$classicVms = Get-AzureVM | select Name, ServiceName, ResourceGroupName
Switch-AzureMode -Name AzureResourceManager
#ServiceName will be blank for these
$armVms = Get-AzureVM | select Name, ServiceName, ResourceGroupName
$allVms = $classicVms + $armVms
$allVms
当您 运行 执行此操作时,您会收到一条警告,提示 Switch-AzureMode 已弃用。
WARNING: The Switch-AzureMode cmdlet is deprecated and will be removed in a future release
弃用是重大更改的一部分。您可以在此处阅读详细信息:Deprecation of Switch-AzureMode.
请注意 Switch-AzureMode 现已弃用 (https://github.com/Azure/azure-powershell/wiki/Deprecation-of-Switch-AzureMode-in-Azure-PowerShell)。 Cmdlet 重命名 Azure 资源管理模块下的所有 cmdlet 都将重命名以符合以下格式:[Verb]-AzureRm[Noun]
示例:New-AzureVm 变为 New-AzureRmVm
使用Azure CLI,我们可以使用az vm list
命令获取当前订阅中所有虚拟机的列表。除此之外,我们只是遍历所有订阅并将结果添加到一个列表中
$results=New-Object -TypeName System.Collections.ArrayList;
$subs = az account list | ConvertFrom-Json;
$subIndex = 0;
foreach ($sub in $subs)
{
$subIndex++;
Write-Progress "Gathering VMs" -Status $sub.name -PercentComplete ($subIndex / $subs.Count * 100)
$vms = az vm list --subscription $sub.id | ConvertFrom-Json
$results.Add(@{
subscription = $sub
vms = $vms
}) > $null;
$results | ConvertTo-Json -Depth 100 > vms.json;
}
然而,does not include the power on/off state of the vms
我们可以使用 az graph query
命令获取所有 VM 信息 + 电源状态。这有更快的好处。需要做一些工作来处理分页结果,但它仍然相当容易。
$query = "
Resources
| where type == 'microsoft.compute/virtualmachines'
| extend PowerState = tostring(properties.extended.instanceView.powerState.code)
| extend vmSize = tostring(properties.hardwareProfile.vmSize)
";
$params = @(
"--graph-query", $query -replace "`n", ""
"--output", "json"
);
$total=New-Object -TypeName System.Collections.ArrayList;
$count=0;
while ($true)
{
$results = az graph query @params | ConvertFrom-Json;
foreach ($d in $results.data)
{
$total.Add($d);
}
$count += $results.count;
$params = @(
"--graph-query", $query -replace "`n", ""
"--output", "json"
"--skip-token", $results.skip_token
"--skip", $count
);
if ([string]::IsNullOrEmpty($results.skip_token)) {break;}
}
$total | ConvertTo-Json -Depth 100 > vms_fast.json;