PowerShell 技能连载 - 用网格视图窗口显示列表视图(第 3 部分)

在前一个技能中我们介绍了 ConvertObject-ToHashTable 函数,它能方便地将对象显示在一个网格视图窗口中。

以下代码是一个改进的版本,能够根据字母顺序排列属性,并且可以由您决定每一列的名称。缺省情况下,Out-GridView 的列名为 “Property” 和 “Value”,但您将它们改为任意名称:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function ConvertObject-ToHashTable
{
param
(
[Parameter(Mandatory,ValueFromPipeline)]
$object,

[string]
$PropertyName = "Property",

[string]
$ValueName = "Value"
)

process
{
$object |
Get-Member -MemberType *Property |
Select-Object -ExpandProperty Name |
Sort-Object |
ForEach-Object { [PSCustomObject]@{
$PropertyName = $_
$ValueName = $object.$_}
}
}
}

Get-ComputerInfo |
# by default, columns are named "Property" and "Value"
ConvertObject-ToHashTable |
Out-GridView


Get-WmiObject -Class Win32_BIOS |
# specify how you'd like to call the columns displayed in a grid view window
ConvertObject-ToHashTable -PropertyName Information -ValueName Data |
Out-GridView

PowerShell 技能连载 - 用网格视图窗口显示列表视图(第 3 部分)

http://blog.vichamp.com/2018/01/25/using-list-view-in-a-grid-view-window-part-3/

作者

吴波

发布于

2018-01-25

更新于

2022-07-06

许可协议

评论