【发布时间】:2020-12-12 22:11:03
【问题描述】:
我正在编写一个脚本来使用 PowerCLI 审核我公司虚拟环境中的磁盘分区类型。到目前为止,我已经得到了这段代码:
$report = @()
$VMs = (Get-VM).where{$_.PowerState -eq 'PoweredOn' -and $_.Guest.OSFullName -match 'Windows'}
foreach ($VM in $VMs){
$vmName = $VM.Name
$output = Invoke-VMScript -ScriptText @'
Get-Disk |
select @{ l="ComputerName"; e={ $env:COMPUTERNAME } },
Number,
@{ name='Size'; expr={[int]($_.Size/1GB)} },
PartitionStyle
'@ -VM $vmName -GuestUser $Username -GuestPassword $Password
$output.ScriptOutput #printing each for testing
$report += $output.ScriptOutput
}
$report | FT -AutoSize
这将产生如下所示的输出:
| ComputerName | Number | Size | PartitionStyle |
|---|---|---|---|
| VMNAME1 | 0 | 100 | MBR |
| VMNAME1 | 1 | 20 | GPT |
| VMNAME1 | 2 | 20 | MBR |
| ComputerName | Number | Size | PartitionStyle |
|---|---|---|---|
| VMNAME2 | 0 | 100 | MBR |
| VMNAME2 | 1 | 20 | GPT |
我面临的问题是输出报告的列标题为每个 VM 重复。我该如何解决这个问题,只显示一次列标题,如下所示:
| ComputerName | Number | Size | PartitionStyle |
|---|---|---|---|
| VMNAME1 | 0 | 100 | MBR |
| VMNAME1 | 1 | 20 | GPT |
| VMNAME1 | 2 | 20 | MBR |
| VMNAME2 | 0 | 100 | MBR |
| VMNAME2 | 1 | 20 | GPT |
关于如何做到这一点的任何想法?我是 powershell 新手,所以我不知道该怎么做。
【问题讨论】:
-
删除或注释掉
$output.ScriptOutput #printing each for testing这一行。使用+-连接到数组也很昂贵。 -
@Theo 删除对它没有影响
标签: powershell powercli