尝试运行连接到每个订阅的脚本,并拉取
$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:计算机名、管理员用户名 windows 配置、 secret "
最终目标是获得脚本,该脚本将为每个订阅打印如下结果:
ResourceGroupName | Name | License Type | VMSize | OS Type
TEST_RG | Test_VM | Windows_Server | DS3_v2 | Windows
Test_RG | Test_VM2 | | DS3_v2 | Linux
等等
感谢您的帮助;很抱歉提出这样的菜鸟问题。花了很多时间试图解决这个问题......
最佳答案
像下面这样的东西就可以了。 您主要缺少的是 calculated properties 。 这允许您执行自定义属性的选择。
一些注释:
在您的代码中,您在 Select 语句上使用了 -WarningAction SilentlyContinue
。您需要将其放在 Get-AzureRMVM
CmdLet 上。
这是我的观点,但除非你是故意写一些俏皮话,否则请尝试更多地为你的代码注入(inject)活力。它将使其更易于阅读、调试和维护。
这是您编写的代码,经过修改以包含计算的属性,并将 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
关于Azure Powershell - 用于跨订阅获取 VM 信息的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53733816/