![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 |
| | #2 (permalink) |
| 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 |
| | #3 (permalink) |
| 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 |
| | #4 (permalink) |
| 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 |
| | #5 (permalink) |
| 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 |
| | #6 (permalink) |
| 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. |
| | #7 (permalink) |
| 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 |
| |
| |
![]() |
| 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 |