powershell - How to custom output of powershell function
问题描述
I am writing a small function to get the file sizes in several pathes. And I have a problem about the function output. Here is my script:
function Get-FileSize {
param (
[Parameter(Mandatory,
ValueFromPipeline)]
[string[]]$Path
)
PROCESS {
foreach ($p in $Path) {
Get-ChildItem -Path $p |
ForEach-Object {
$size = (Get-ChildItem -LiteralPath $PSItem -Recurse | Measure-Object -Sum Length).Sum + 0
[PSCustomObject]@{
Name = $_.Name
Size = $size
}
}
}
}
}
This function will output several pscustomobjects. The size property is very hard to read, so I format the size
property like this:
Size = "{0:N2} GB" -f $size
While this version is very easy to read, I can not be sort through the size
property because the type of this property is string
.
My problem is how can I custom the display style of pscustomobject's property?
For example, the command Get-Volume
has a nice output.
解决方案
After reading related topic about Format.ps1xml mentioned by @Olaf, I have solved the problem.
First, I create a customized Format.ps1xml
:
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<ViewDefinitions>
<View>
<Name>FileSizeView</Name>
<ViewSelectedBy>
<TypeName>MyType.FileSizeInfo</TypeName>
</ViewSelectedBy>
<GroupBy>
<PropertyName>Path</PropertyName>
</GroupBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Name</Label>
<Width>50</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Size</Label>
<Width>15</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Name</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
if ($_.Size -lt 1KB) {
$_.Size
} elseif (($_.Size -ge 1KB) -and ($_.Size -lt 1MB)) {
"{0:N2} KB" -f ($_.Size / 1KB)
} elseif (($_.Size -ge 1MB) -and ($_.Size -lt 1GB)) {
"{0:N2} MB" -f ($_.Size / 1MB)
} else {
"{0:N2} GB" -f ($_.Size / 1GB)
}
</ScriptBlock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
Second, add the new format file to the current session Update-FormatData \path\to\file\MyView.Format.ps1xml
.
Third, before I return the pscustomobject in my function, I add the TypeName
to it. The TypeName
is defined in node <ViewSelectedBy>
. Here is the function:
function Get-FileSize {
param (
[Parameter(Mandatory,
ValueFromPipeline)]
[string[]]$Path
)
PROCESS {
foreach ($p in $Path) {
Get-ChildItem -Path $p |
ForEach-Object {
$size = (Get-ChildItem -LiteralPath $PSItem -Recurse -File | Measure-Object -Sum Length).Sum + 0
$object = [PSCustomObject]@{
Name = $_.Name
Size = $size
Path = $p
}
$object | Add-Member -TypeName 'MyType.FileSizeInfo'
return $object
}
}
}
}
推荐阅读
- r - 基于多个其他列对 R 中的多个列求和
- java - 在没有连接的情况下使用 Hibernate 4 生成 DDL
- mysql - 如何优化这个查询?慢查询
- android - 约束布局中缺少约束
- c++ - 正在移动 std::bitset
超过 N 个职位未定义的行为? - ios - 反应本机套接字未在发布版本(IOS)中初始化
- python - 使用 numpy.max/ numpy.min 作为时间戳值
- apache-spark - 在一个 Spark 作业中可以有多个 Spark 结构化流式写入查询(90+)吗?
- c - AES256 hex_print 到 hex_tofile 使用 enc_out
- php - hasMany 模型关系返回空数组。lLaravel 框架 5.5.40