![如何在不使用.format.ps1xml 文件的情况下强制将自定义 PSObject 输出为“Format-Table”?](https://linux22.com/image/1520472/%E5%A6%82%E4%BD%95%E5%9C%A8%E4%B8%8D%E4%BD%BF%E7%94%A8.format.ps1xml%20%E6%96%87%E4%BB%B6%E7%9A%84%E6%83%85%E5%86%B5%E4%B8%8B%E5%BC%BA%E5%88%B6%E5%B0%86%E8%87%AA%E5%AE%9A%E4%B9%89%20PSObject%20%E8%BE%93%E5%87%BA%E4%B8%BA%E2%80%9CFormat-Table%E2%80%9D%EF%BC%9F.png)
我知道我可以MyCustomPSObject
通过生成和加载MyCustomPSObject.format.ps1xml
文件来定义我的格式。
我已经看到人们在.ps1脚本文件内部即时完成这种格式文件生成。(这里也开始列出句子下面“所以我编写了一个名为 Get-ProcessTotal 的高级 PowerShell 函数”从 145 行开始。
据我了解,该格式文件中的第一个条目将用作默认值。
生成该外部文件对我来说似乎很麻烦。
问题:
- 有没有办法做到这一点而不生成和加载外部文件?
我想要的是“将其输出为表格”. (基本上是贯穿始终Format-Table
但在此过程中不会丢失对象性。)
相关问题
脚本 Get-VSSWriters.Current-Version.ps1
这是有问题的脚本:
# This script lists VSS writers.
# The original, uncustomized, version was taken from here:
# https://gallery.technet.microsoft.com/scriptcenter/Powershell-ScriptFunction-415e9e70
$RawWriters = Invoke-Command -ErrorAction Stop -ScriptBlock {
#Requires -RunAsAdministrator
return (VssAdmin List Writers)
}
# We don't really care about "WriterID" and "WriterInstanceID", so we leave them out of the default
# display set. (Approach copied from here:
# https://learn-powershell.net/2013/08/03/quick-hits-set-the-default-property-display-in-powershell-on-custom-objects/ )
$defaultDisplaySet = 'WriterEnum', 'WriterName', 'StateID', 'StateDesc', 'LastError'
$defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet', [string[]]$defaultDisplaySet)
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet)
$Writers = @()
for ($i=0; $i -lt (($RawWriters.Count)-3)/6; $i++) {
$Writer = New-Object -TypeName psobject
$Writer.PSObject.TypeNames.Insert(0, 'MyVSSWriter')
$Writer | Add-Member MemberSet PSStandardMembers $PSStandardMembers
$Writer| Add-Member "WriterEnum" ($i + 1)
# Note: We split on both single quotes and double quotes.
# That way this script will work for both:
# * the English language version of Windows (>>Writer name: 'WMI Writer'<<)
# * the German version of Windows (>>Verfassername: "WMI Writer"<<)
$Writer| Add-Member "WriterName" $RawWriters[($i*6)+3].Split("`"'")[1]
$Writer| Add-Member "WriterID" $RawWriters[($i*6)+4].Split('{}')[1]
$Writer| Add-Member "WriterInstanceID" $RawWriters[($i*6)+5].Split('{}')[1]
$Writer| Add-Member "StateID" $RawWriters[($i*6)+6].Split('[]')[1]
$Writer| Add-Member "StateDesc" $RawWriters[($i*6)+6].Split('[]')[2].SubString(1)
$Writer| Add-Member "LastError" $RawWriters[($i*6)+7].Split(':')[1].Substring(1)
$Writers += $Writer
}
$Writers
实际产量
PS C:\> .\Get-VSSWriters.Current-Version.ps1 | select -first 1
WriterEnum : 1
WriterName : Task Scheduler Writer
StateID : 1
StateDesc : Stable
LastError : No error
期望输出
PS C:\> .\Get-VSSWriters.Updated-Version.ps1 | select -first 1
WriterEnum WriterName StateID StateDesc LastError
---------- ---------- ------- --------- ---------
1 Task Scheduler Writer 1 Stable No error
(应该与 相同.\Get-VSSWriters.Current-Version.ps1 | select -first 1 | Format-Table
。)