Powershell输出未正确格式化

时间:2016-10-07 15:53:11

标签: powershell

我正在使用此脚本从HyperV群集上的虚拟机获取一些基本信息:

#Establish global variables and MasterList array
$VMList = Get-VM
$MasterList = @()

#Loop through VMs and get Name, Processor count, assigned memory, add to MasterList
foreach($vm in $VMList) {
$ALLVHD = Get-VHD $vm.harddrives.path -ComputerName $vm.computername
$MyObject = New-Object PSObject -Property @{
    Name = ($vm).VMName
    ProcessorCount = (Get-VMProcessor $vm).Count
    AssignedMemory = ($vm).MemoryAssigned
    DiskType = $VHD.VhdType
    'Total(GB)' = [math]::Round($VHD.Size/1GB)
    'Used(GB)' = [math]::Round($VHD.FileSize/1GB)
    'Free(GB)' =  [math]::Round($VHD.Size/1GB- $VHD.FileSize/1GB)
}

$MasterList += $MyObject
}
$MasterList | Out-GridView 

它主要起作用,但有几个问题。列顺序错误,它输出DiskType,Name,AssignedMemory,Free(GB),ProcessorCount,Used(GB),Total(GB),我不知道为什么,因为现在它是如何在代码中排序的。此外,如果不正确,所有项目的免费,已用和总金额为71,29和100。

如果任何Powershell专家可以帮助我,我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

我想通了,谢谢你的建议

#Establish global variables and MasterList array
$VMList = Get-VM
$MasterList = @()

#Loop through all VMs on node
foreach($vm in $VMList) {

    $ALLVHD = Get-VHD $vm.HardDrives.path -ComputerName $vm.computername

    foreach($VHD in $ALLVHD){
                $MyObject = New-Object PSObject -Property @{
                    Name = $vm.Name
                    DiskType = $VHD.VhdType
                    Path = $VHD.Path
                    'Total(GB)' = [math]::Round($VHD.Size/1GB)
                    'Used(GB)' = [math]::Round($VHD.FileSize/1GB)
                    'Free(GB)' =  [math]::Round($VHD.Size/1GB- $VHD.FileSize/1GB)
                    ProcessorCount = (Get-VMProcessor $vm).Count
                    AssignedMemory = ($vm).MemoryAssigned        
                 }
    #Add information to MasterList array
    $Masterlist += $MyObject
    }
}

#Change this line to print output however you want
$MasterList | select Name,DiskType,Path,'Total(GB)','Used(GB)','Free(GB)',ProcessorCount,@{Expression={$_.AssignedMemory/1GB};Label="AssignedMemory(GB)"}