【问题标题】:How to get list of all Azure VMs in Powershell如何在 Powershell 中获取所有 Azure VM 的列表
【发布时间】:2015-09-30 16:24:54
【问题描述】:

我正在尝试在 Powershell 中获取所有 Azure VM 的列表。

Get-AzureVM

很遗憾,这只返回虚拟机(经典)下列出的虚拟机。

如何获取新虚拟机的列表?

【问题讨论】:

    标签: powershell azure virtual-machine


    【解决方案1】:

    根据大卫的回答,我编写了以下脚本,结合了两个虚拟机列表:

    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

    【讨论】:

    • 不错。 +1。另外,感谢您指出弃用警告。
    【解决方案2】:

    您需要使用 Azure 资源管理器模式来访问新的虚拟机:

    Switch-AzureMode -Name AzureResourceManager
    

    您可以阅读更多关于它的信息here

    【讨论】:

      【解决方案3】:

      请注意,Switch-AzureMode 现在已被弃用 (https://github.com/Azure/azure-powershell/wiki/Deprecation-of-Switch-AzureMode-in-Azure-PowerShell)。 Cmdlet 重命名 Azure 资源管理模块下的所有 cmdlet 将被重命名以适应以下格式:[动词]-AzureRm[名词]

      示例:New-AzureVm 变为 New-AzureRmVm

      【讨论】:

        【解决方案4】:

        使用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;
        

        Note that we use array splatting instead of object splatting

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-02-14
          • 1970-01-01
          • 2012-08-10
          • 2022-10-17
          • 2018-03-21
          • 2015-11-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多