此示例演示如何实现将列表行分隔为组的列表视图。 此列表视图显示 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