此範例示範如何實作清單檢視,以顯示清單每個數據列的自定義標籤。 此清單檢視會顯示由 Get-Service Cmdlet 所傳回 System.ServiceProcess.ServiceController 物件的屬性。 如需清單檢視元件的詳細資訊,請參閱 建立清單檢視。
載入此格式檔案
將本主題的範例區段的 XML 複製到文字檔中。
儲存文字檔。 請務必將
format.ps1xml擴展名新增至檔案,以將其識別為格式化檔案。開啟 Windows PowerShell,然後執行下列命令,將格式化檔案載入目前的工作階段:
Update-FormatData -PrependPath PathToFormattingFile。
警告
此格式化檔案會定義 Windows PowerShell 格式化檔案已定義的物件顯示。 當您執行 Cmdlet 時,必須使用
PrependPath參數,而且無法將此格式化檔案載入為模組。
演示
此格式化檔案示範下列 XML 元素:
檢視 Name 專案。
ViewSelectedBy 元素,定義檢視所顯示的物件。
ListControl 元素,定義檢視所顯示的屬性。
ListItem 元素,定義清單檢視列中顯示的專案。
Label 元素,定義清單檢視列中顯示的內容。
PropertyName 元素,定義要顯示的屬性。
範例
下列 XML 會定義清單檢視,以在每個資料列中顯示自定義標籤。 在此情況下,標籤會包含每個字母大寫的屬性名稱,以及 「property」 這個字。 在每個數據列中,屬性的名稱會顯示,後面接著 屬性的值。
<Configuration>
<ViewDefinitions>
<View>
<Name>System.ServiceProcess.ServiceController</Name>
<ViewSelectedBy>
<TypeName>System.ServiceProcess.ServiceController</TypeName>
</ViewSelectedBy>
<ListControl>
<ListEntries>
<ListEntry>
<ListItems>
<ListItem>
<Label>NAME property</Label>
<PropertyName>Name</PropertyName>
</ListItem>
<ListItem>
<Label>DISPLAYNAME property</Label>
<PropertyName>DisplayName</PropertyName>
</ListItem>
<ListItem>
<Label>STATUS property</Label>
<PropertyName>Status</PropertyName>
</ListItem>
<ListItem>
<Label>SERVICETYPE property</Label>
<PropertyName>ServiceType</PropertyName>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
</ListControl>
</View>
</ViewDefinitions>
</Configuration>
下列範例示範 Windows PowerShell 如何顯示載入此格式檔案之後 System.ServiceProcess.ServiceController 物件。
Get-Service f*
NAME property : Fax
DISPLAYNAME property : Fax
STATUS property : Stopped
SERVICETYPE property : Win32OwnProcess
NAME property : FCSAM
DISPLAYNAME property : Microsoft Antimalware Service
STATUS property : Running
SERVICETYPE property : Win32OwnProcess
NAME property : fdPHost
DISPLAYNAME property : Function Discovery Provider Host
STATUS property : Stopped
SERVICETYPE property : Win32ShareProcess
NAME property : FDResPub
DISPLAYNAME property : Function Discovery Resource Publication
STATUS property : Running
SERVICETYPE property : Win32ShareProcess
NAME property : FontCache
DISPLAYNAME property : Windows Font Cache Service
STATUS property : Running
SERVICETYPE property : Win32ShareProcess
NAME property : FontCache3.0.0.0
DISPLAYNAME property : Windows Presentation Foundation Font Cache 3.0.0.0
STATUS property : Stopped
SERVICETYPE property : Win32OwnProcess
NAME property : FSysAgent
DISPLAYNAME property : Microsoft Forefront System Agent
STATUS property : Running
SERVICETYPE property : Win32OwnProcess
NAME property : FwcAgent
DISPLAYNAME property : Firewall Client Agent
STATUS property : Running
SERVICETYPE property : Win32OwnProcess
另請參閱
格式化檔案 範例