【问题标题】:Azure Powershell - Script to obtain VM info across subscriptionsAzure Powershell - 跨订阅获取 VM 信息的脚本
【发布时间】:2018-12-11 23:17:57
【问题描述】:

尝试运行将连接到每个订阅的脚本,并拉取

$azureSubs = Get-AzureRMSubscription
$azureSubs | ForEach-Object {Select-AzureRMSubscription $_ | Out-Null; Get-AzureRMVM | select resourcegroupname, name, licensetype -WarningAction SilentlyContinue}

这可行,但我想再添加两条信息:“OSType”和“VMSize”

如果我执行 GET-AZURERMVM,在运行命令的订阅表中,我需要两条信息:VmSize 和 OsType

但是,当我尝试将它们添加到查询中时,这些列是空白的。 我相信 VmSize 在 HardwareProfile 中,而 OsType 在 OsProfile 中,就像我运行“Get-AzureRMVM -name (name) -resourcegroupname (RGname)”一样,然后它显示“Hardware Profile: VMSize”和“OSProfile: ComputerName, AdminUsername windowsConfiguration, Secrets"

最终目标是获得脚本,该脚本将为每个订阅打印结果,例如:

ResourceGroupName  |  Name | License Type | VMSize | OS Type
TEST_RG | Test_VM | Windows_Server | DS3_v2 | Windows
Test_RG | Test_VM2 |     |  DS3_v2 | Linux

等等

感谢您的帮助;抱歉这样的菜鸟问题。花了这么多时间试图解决这个问题......

【问题讨论】:

    标签: azure powershell


    【解决方案1】:

    类似下面的东西会起作用。 您缺少的主要是calculated properties。 这使您可以选择自定义属性。

    一些注意事项:

    在您的代码中,您在 Select 语句中使用了-WarningAction SilentlyContinue。您需要将其放在 Get-AzureRMVM CmdLet 上。

    这是我的观点,但除非您是出于目的编写单行代码,否则请尝试更多地为您的代码充气。它将使其更易于阅读、调试和维护。

    这是您编写的代码,已修改为包含计算的属性,并且将 WarningAction 参数设置为 Get-AzureRMVM 而不是 Select 语句。

    $azureSubs = Get-AzureRMSubscription
    $Vms = $azureSubs | ForEach-Object {Select-AzureRMSubscription $_ | Out-Null; Get-AzureRMVM -WarningAction SilentlyContinue | select resourcegroupname, name, licensetype,  @{Name="VMSize";Expression={$_.HardwareProfile.VmSize}},@{Name="OsType";Expression={$_.StorageProfile.OsDisk.OsType}}}
    $Vms | ft 
    

    同样的事情,有一些进度指示,而不是把所有东西都放在一条线上。

    $azureSubs = Get-AzureRMSubscription
    $Vms = New-Object 'System.Collections.Generic.List[PSObject]'
    ForEach ($sub in $azureSubs) {
        Select-AzureRMSubscription $sub | Out-Null  
        Write-Host "Processing Subscription $($sub.Name)".PadRight(50,' ') -ForegroundColor Cyan -NoNewline
        [PsObject[]]$items = Get-AzureRMVM -WarningAction SilentlyContinue | 
            select resourcegroupname,
               name, 
               licensetype,
                @{Name="VMSize";Expression={$_.HardwareProfile.VmSize}}, 
                @{Name="OsType";Expression={$_.StorageProfile.OsDisk.OsType}}
    
            Write-Host "($($items.count) retrieved)"
            if ($items -ne $null) {
                $vms.AddRange($items)
            }
    }
    
    
    $vms | Format-Table 
    

    【讨论】:

      【解决方案2】:

      您正在select 方面寻找类似的东西

      select resourcegroupname, name, licensetype, @{Name="VMSize";Expression={$_.HardwareProfile.VmSize}}, @{Name="OsType";Expression={$_.StorageProfile.OsDisk.OsType}}

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-14
        • 1970-01-01
        • 2018-02-05
        • 1970-01-01
        • 1970-01-01
        • 2019-05-05
        相关资源
        最近更新 更多