Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

How to use the ETS to handle complex .Net types in a CmdLet?

Closed Thread
 
Thread Tools Display Modes
Old 11-08-2006   #1 (permalink)
Joannes Vermorel
Guest


 

How to use the ETS to handle complex .Net types in a CmdLet?

Let say that I have a pre-defined class

public class Book
{
public string Title { get; set; }
public string Author { get; set; }
public string ISBN { get; set; }
}

Let say that I am writing a CmdLet named Get-BookPrice that takes a Book as
input pipeline parameter and output the price (thanks a DB, whatever).

How I am supposed to do that with PowerShell?

The naive option consists in creating a CmdLet taking 3 string parameters.
This approach does not look very smart considering the ETS.

Yet with the documentation available at
http://windowssdk.msdn.microsoft.com.../ms714419.aspx
I am a bit at a loss, and do not know what to do.

Am-I supposed to
- Write a CmdLet that takes a PSObject as parameter? or is-it directly a
Book?
- Write a PsTypeConverter? From string to Book? From PSObject to Book?

Thanks in advance,
Joannes
http://www.peoplewords.com
Old 11-08-2006   #2 (permalink)
Adam Milazzo
Guest


 

Re: How to use the ETS to handle complex .Net types in a CmdLet?

I don't know what ETS is, but CmdLets can just take an object of type
Book. All the normal PowerShell type conversions apply, so if the book
has a static Parse(string) method, PowerShell will use it to convert
from string to Book...

Joannes Vermorel wrote:
> Let say that I have a pre-defined class
>
> public class Book
> {
> public string Title { get; set; }
> public string Author { get; set; }
> public string ISBN { get; set; }
> }
>
> Let say that I am writing a CmdLet named Get-BookPrice that takes a Book as
> input pipeline parameter and output the price (thanks a DB, whatever).
>
> How I am supposed to do that with PowerShell?
>
> The naive option consists in creating a CmdLet taking 3 string parameters.
> This approach does not look very smart considering the ETS.
>
> Yet with the documentation available at
> http://windowssdk.msdn.microsoft.com.../ms714419.aspx
> I am a bit at a loss, and do not know what to do.
>
> Am-I supposed to
> - Write a CmdLet that takes a PSObject as parameter? or is-it directly a
> Book?
> - Write a PsTypeConverter? From string to Book? From PSObject to Book?
>
> Thanks in advance,
> Joannes
> http://www.peoplewords.com

Old 11-09-2006   #3 (permalink)
Joannes Vermorel
Guest


 

Re: How to use the ETS to handle complex .Net types in a CmdLet?

> I don't know what ETS is, but CmdLets can just take an object of type
> Book. All the normal PowerShell type conversions apply, so if the book
> has a static Parse(string) method, PowerShell will use it to convert
> from string to Book...


Yes, but precisely, I was focusing on *complex* (multiple properties)
objects. Thus, straightforward conversion string==>MyObject is not really
practical (you end-up with connection-string like custom syntax and loose the
benefits of strong typing).

Joannes
Old 11-09-2006   #4 (permalink)
Maximilian Hänel
Guest


 

Re: How to use the ETS to handle complex .Net types in a CmdLet?

Hi Joannes,

> Let say that I have a pre-defined class
>
> public class Book
> {
> public string Title { get; set; }
> public string Author { get; set; }
> public string ISBN { get; set; }
> }
>
> Let say that I am writing a CmdLet named Get-BookPrice that takes a Book as
> input pipeline parameter and output the price (thanks a DB, whatever).
>
> How I am supposed to do that with PowerShell?


I haven't written a CmdLet but what about creating a cmdlet Create-Book
so you can write something like this:

$book=Create-Book -Title "Powershell" -Author "Unknown" -ISBN "Whatever"
Get-BookPrice $book

or like this

Create-Book -Title "Powershell" -Author "Unknown" -ISBN "Whatever" |
Get-BookPrice

and so on..

cu

Max
Old 11-09-2006   #5 (permalink)
dreeschkind
Guest


 

Re: How to use the ETS to handle complex .Net types in a CmdLet?

"Adam Milazzo" wrote:

> I don't know what ETS is, <snip>


Just in case you're interested: ETS = Extended Type System

http://msdn2.microsoft.com/en-us/library/ms714419.aspx
http://msdn2.microsoft.com/en-us/library/ms714665.aspx

--
greetings
dreeschkind
Old 11-09-2006   #6 (permalink)
Joannes Vermorel
Guest


 

Re: How to use the ETS to handle complex .Net types in a CmdLet?

> I haven't written a CmdLet but what about creating a cmdlet Create-Book
> so you can write something like this:
>
> $book=Create-Book -Title "Powershell" -Author "Unknown" -ISBN "Whatever"
> Get-BookPrice $book


Yes, this is indeed a good approach. Thanks.

Joannes

Ps1: the common verb would be 'New' here :-)
Ps2: it seems that I am not the only one having little insights on ETS.
Old 11-09-2006   #7 (permalink)
Maximilian Hänel
Guest


 

Re: How to use the ETS to handle complex .Net types in a CmdLet?

Hi Joannes,

> Yes, this is indeed a good approach. Thanks.


You're welcome!

> Ps1: the common verb would be 'New' here :-)


According to Bob Ross:
"We don't make mistakes here, we just have happy accidents" ;-)

cu

Max
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Invoking Cmdlet Get-Location from cmdlet,cant get Currnt Directory Vikram PowerShell 2 06-05-2008 04:41 AM
Whether a cmdlet derives from cmdlet or pscmdlet Marco Shaw PowerShell 1 09-19-2007 09:18 PM
complex variable bdsherman@gmail.com PowerShell 6 02-22-2007 11:01 AM
Control.Handle - How do I get a handle to a control with WinFX iNSaNey Avalon 2 05-14-2006 06:51 PM








Vistax64.com 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 2005-2008

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 47 48 49 50