Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - types.custom.ps1xml for custom members

Reply
 
Old 02-25-2008   #1 (permalink)


Vista Ultimate x64
 
 

types.custom.ps1xml for custom members

Does anyone have examples of a types.custom.ps1xml file working correctly? I am aware of

Precision Computing - Add Custom Methods and Properties to Types in PowerShell

but I want more examples. Here's my experimental types.custom.ps1xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid XML Studio 1.0.8.0 -->
<Types><Type><Name>System.ServiceProcess.ServiceController</Name><Members><ScriptProperty><Name>LeanStartupType</Name><GetScriptBlock>{ $this.LeanStartupType }</GetScriptBlock><SetScriptBlock>{ if (($args[0] -eq "") -or ($args[0] -eq "Auto") -or
($args[0] -eq "Manual") -or ($args[0] -eq "Disabled")) {
$this.LeanStartupType = $args[0] } else { Write-Host ("Error:
LeanStartupType can only be Auto, Manual, Disabled, or the empty string") } }
</SetScriptBlock></ScriptProperty><ScriptProperty><Name>RichStartupType</Name><GetScriptBlock>{ $this.RichStartupType }</GetScriptBlock><SetScriptBlock>{ if (($args[0] -eq "") -or ($args[0] -eq "Auto") -or
($args[0] -eq "Manual") -or ($args[0] -eq "Disabled")) {
$this.RichStartupType = $args[0] } else { Write-Host ("Error:
RichStartupType can only be Auto, Manual, Disabled, or the empty string.") } }
</SetScriptBlock></ScriptProperty><AliasProperty><Name>Min</Name><ReferencedMemberName>LeanStartupType</ReferencedMemberName></AliasProperty><AliasProperty><Name>Max</Name><ReferencedMemberName>RichStartupType</ReferencedMemberName></AliasProperty><ScriptMethod><Name>Initialize</Name><Script>{ write-host "Initializing " + "$this.Name"; $this.Min = ""; $this.Max = "" }</Script></ScriptMethod></Members></Type>
</
Types>
It does indeed add members to the objects returned by the Get-Service commandlet, but I am not able to make Get, Set, or the Initialize script method do anything useful.
For example, after loading my custom types:

[1][4]>get-service | get-member

TypeName: System.ServiceProcess.ServiceController
Name MemberType Definition
---- ---------- ----------
Max AliasProperty Max = RichStartupType
Min AliasProperty Min = LeanStartupType
Name AliasProperty Name = ServiceName
add_Disposed Method System.Void add_Disposed(EventHandler value)
Close Method System.Void Close()
Continue Method System.Void Continue()
...........
Status Property System.ServiceProcess.ServiceControllerStatus Status {get;}
Initialize ScriptMethod System.Object Initialize();
LeanStartupType ScriptProperty System.Object LeanStartupType {get={ $this.LeanStartupType };set={ if (($ar...
RichStartupType ScriptProperty System.Object RichStartupType {get={ $this.RichStartupType };set={ if (($ar...

All well and good so far; the lavender items are my customizations. But

[1][6]>foreach ($serv in Get-Service) {$serv.Initialize}

simply writes the same junk again and again:

Script : { write-host "Initializing " + "$this.Name"; $this.Min = ""; $this.Max = "" }
OverloadDefinitions : {System.Object Initialize();}
MemberType : ScriptMethod
TypeNameOfValue : System.Object
Value : System.Object Initialize();
Name : Initialize
IsInstance : False

I don't want to see that 50 times! I just want it to run the damn script.

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Custom TraceListener using custom LogEntry .NET General
Accessing custom types output from custom cmdlet's in C# GUI PowerShell
custom types for System.Int32 PowerShell
Unable to associate file types to custom application Vista file management


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46