Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Does anyone have experience using Update-FormatData?

13 views
Skip to first unread message

Bob Landau

unread,
Mar 17, 2009, 1:42:15 PM3/17/09
to
Briefly what I'm after is for Get-Service to return the "StartType" of a
service. Paul explained to me why Get-Service currently isn't doing this.

However I still I don't like its behavior.

What I want to do is make a suggestion to Microsoft to add this. But before
this I want to show them how to do this. At worst I'll have the feature I
want and I really do think "handing" them code for this will increase my
chances of this getting in to Powershell.

There are two thing I need to do

1) Create a customized type.ps1xml file for
[System.ServiceProcess.ServiceController].

This has been done now either

Get-Service | % {$_.StartType}
or
Get-Service | Format-List *

will display whether each service is demand, auto, ...

2) Create a customized format.ps1xml file for the above type so that the
default "formater" will display this property. This I'm having a hard time
with. There is next to no documentation on this and I see no way to debug or
track down what is going wrong here.

Below are the XML files that I've used for each step

1) type.sc.ps1xml
<Types>
<Type>
<Name>System.ServiceProcess.ServiceController</Name>
<Members>
<ScriptProperty>
<Name>StartType</Name>
<GetScriptBlock>
switch ( ((Get-ItemProperty -Path
HKLM:\system\currentcontrolset\Services\$($this.Name) -Name Start)).Start )
{
0 {"Boot_Start"}
1 {"System_Start"}
2 {"Auto_Start"}
3 {"Demand_Start"}
4 {"Disabled"}
}
</GetScriptBlock>
</ScriptProperty>
</Members>
</Type>
</Types>

That really is all there is to this.

Below is my attempt at customizing what the format of this type should be.
To keep this post as small as possible I'm only showing the XML elment that
I've added. The complete listing for this type is in DotNetTypes.Format.ps1xml

I have modeled this after the system file Certificate.Format.ps1xml which is
the only file that I see using a "scriptblock" in a <ListItem>

While the below file is not generating any errors when processed by
Update-FormatData. I'm not seeing the property StartType which I'm after.


2) <Configuration>
<ViewDefinitions>
<View>
<Name>System.ServiceProcess.ServiceController</Name>
<ViewSelectedBy>
<TypeName>System.ServiceProcess.ServiceController</TypeName>
</ViewSelectedBy>
<ListControl>
<ListEntries>
<ListEntry>
<ListItems>
..... add the existing properties here from
<ListItem>
<ScriptBlock>
$_.StartType
</ScriptBlock>
<Label>StartType</Label>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
</ListControl>
</View>
</ViewDefinitions>
</Configuration>


thx
bob


RickB

unread,
Mar 17, 2009, 2:33:00 PM3/17/09
to
On Mar 17, 12:42 pm, Bob Landau <BobLan...@discussions.microsoft.com>
wrote:

It sounds like you didn't modify the grouping format to include your
new property

Kiron

unread,
Mar 17, 2009, 4:17:48 PM3/17/09
to
# remove the ScriptBlock node from your custom format ps1xml file
<ListItem>
<ScriptBlock>
$_.StartType
</ScriptBlock>
<Label>StartType</Label>
</ListItem>

# is enough with the property name in the Label node
<ListItem>
<Label>StartType</Label>
</ListItem>


Update-TypeData .\Service.Type.ps1xml
# prepend the ps1xml path before the type's default is loaded
Update-FormatData -PrependPath .\Service.Format.ps1xml
gsv

--
Kiron

Bob Landau

unread,
Mar 17, 2009, 6:22:09 PM3/17/09
to
Thanks Rick and Kiron

Rick I've looked and do see "-GroupingFormat" but I just don't know how to
use this

Kiron,

I did see Append/PrependPath but just didn't understand how either of these
would affect the loading of my format file.

-WhatIf

is worthless and while the Powershell engine will let you easily debug the
<mytype>.ps1xml it won't give you a clue how "debug" or even "printf" type
stuf with the <myformat> siblings.

What I've done is two things which at least gets me past my
mis-understanding on how to create customized format files.

1) As you said I've removed the <scriptblock> in addition I needed to change
the <label> to <propertyname>

2) specified -prepend

ideally I'd like to maintain the same defaults that PS does for this type.

i.e

# without modification this
gsv
#and this with modification are the same
gsv | ft

and

#without modification
gsv | fl *

is more equvialment to
gsv

but I can live with this. At least I have now have something concrete to
show to Microsoft when I try and persuade them to add this modification to
the output of Get-Service

Later when I have more time I'll look into this which is probably a simple
modification of what I have.


thx again to both of you
bob

Kiron

unread,
Mar 17, 2009, 8:24:56 PM3/17/09
to
Format-* Cmdlets' Views are separate, i.e. their structures are similar but different.
At the bottom of this post is the content of Service.Format.ps1xml (I've attached it as well)

If you want to override the type's default views, pass the ps1xml file's path to -PrependPath, the default view is available, to use it, pass its Name through the Format-*'s -View:

# new PS session
Update-TypeData .\Service.Type.ps1xml
Update-FormatData -PrependPath .\Service.Format.ps1xml

# to use the new Table View
gsv
# to use the default Table View
gsv | ft -view service
# to use the new List View
gsv | fl
# to use the default List View
gsv | fl -view System.ServiceProcess.ServiceController

If you don't want to override the default views pass the ps1xml file's path to -AppendPath, the custom views can be selected through the Format-*'s -View parameter:

# new PS session
Update-TypeData .\Service.Type.ps1xml
Update-FormatData .\Service.Format.ps1xml
# to use the new Table View
gsv | ft -view MyView
# to use the new List View
gsv | fl -view MyView

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# -< Service.Format.ps1xml >-
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<ViewDefinitions>
<View>
<Name>MyView</Name>
<ViewSelectedBy>
<TypeName>System.ServiceProcess.ServiceController</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Width>12</Width>
</TableColumnHeader>
<TableColumnHeader>
<Width>8</Width>
</TableColumnHeader>
<TableColumnHeader>
<Width>18</Width>
</TableColumnHeader>
<TableColumnHeader>
<Width>38</Width>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>StartType</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Status</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Name</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>DisplayName</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>MyView</Name>
<ViewSelectedBy>
<TypeName>System.ServiceProcess.ServiceController</TypeName>
</ViewSelectedBy>
<ListControl>
<ListEntries>
<ListEntry>
<ListItems>
<ListItem>
<PropertyName>Name</PropertyName>
</ListItem>
<ListItem>
<PropertyName>DisplayName</PropertyName>
</ListItem>
<ListItem>
<PropertyName>Status</PropertyName>
</ListItem>
<ListItem>
<PropertyName>DependentServices</PropertyName>
</ListItem>
<ListItem>
<PropertyName>ServicesDependedOn</PropertyName>
</ListItem>
<ListItem>
<PropertyName>CanPauseAndContinue</PropertyName>
</ListItem>
<ListItem>
<PropertyName>CanShutdown</PropertyName>
</ListItem>
<ListItem>
<PropertyName>CanStop</PropertyName>
</ListItem>
<ListItem>
<PropertyName>ServiceType</PropertyName>
</ListItem>
<ListItem>
<PropertyName>StartType</PropertyName>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
</ListControl>
</View>
</ViewDefinitions>
</Configuration>
# -< Service.Format.ps1xml >-

--
Kiron
Service.Format.ps1xml
0 new messages