Amure在azure devops管道中尝试使用azure powershell任务获取VM sku详细信息以及其余参数。但不是vmsku详细信息,而是在excel工作表的所有列中显示“ Microsoft.Azure.management.Compute.Models.HardwareProfile”的excel工作表。我在这里做错了什么。希望获得诸如“ Standard_m32ls”之类的详细信息
注意:grep-wildcard变量alue将通过管道传递
report = @()
$niclist = Get-AZNetworkInterface | Where-Object {$_ResourceGroupName -eq '*$(grp-wildcard)' } |Select-Object
$VM =Get-AzVm -Status | Where-Object {$_ResourceGroupName -eq '*$(grp-wildcard) '} |Select-Object
$power= $VM.powerstate
$vms = Get-AzVM | Where-Object {$_ResourceGroupName -eq '*$(grp-wildcard) '} |Select-Object
foreach($nic in $niclist){
Array = "" |Select-Object ResourceGroupName,ServerHostname,VMstatus,VMSKU
$Array.ResourceGroupName = $nic.ResourceGroupName
$Array.ServerHostName =$nic.VirtualMachine.Id
$Array.VMstate = $power[0]
$Array.VMSKU = $vms.HardwareProfile.VmSize[0]
}
$report+=$Array
$report |Format-Table ResourceGroupName,ServerHostname,VMstatus,VMSKU
$report | Export-Csv -NTI -path "$(BuildArtifactStagingDirectory)/test.csv"
答案 0 :(得分:1)
此脚本获取的数据与您尝试执行的操作相同:
$rgName = ""
$vms = Get-AzVM -Status | Where-Object { $_.ResourceGroupName -eq $rgName }
$report = @()
foreach ($vm in $vms) {
$vmResult = @{
ResourceGroupName = $vm.ResourceGroupName
ServerHostName = $vm.Id
VmStatus = $vm.Powerstate
VmSku = $vm.HardwareProfile.VmSize
}
$report += $vmResult
}
$report | Export-Csv -Path "$(BuildArtifactStagingDirectory)/test.csv"
答案 1 :(得分:0)
这看起来正在满足您的需求
$rgName = ""
$report = @()
Get-AzVm -Status | Select-Object @{N="ServerHostName"; E={$_.VmId}}, Name, ResourceGroupName, @{N="VmSku"; E={$_.HardwareProfile.VmSize}}, @{N="VmStatus"; E={$_.Powerstate}} | Where-Object { $_.ResourceGroupName -eq $rgName } | ForEach-Object $vm {
$report += $vm
}
$report | Export-Csv -Path "$(BuildArtifactStagingDirectory)/test.csv"