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