我知道我可以MyCustomPSObject
透過產生和載入MyCustomPSObject.format.ps1xml
文件來定義格式。
而且我已經看到了人們從 .ps1-script 檔案內部即時產生格式檔案。(也從這裡開始列在句子下面“所以我寫了一個名為 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
。)