![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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 | Parsing command line arguments I m pretty new to powershell and exploring its capabilities. One of the things i m trying to experiment is creating a custom cmdlets. This is what i m thinking of so please let me know if it is possible. I have a custom cmdlet ParseCommandline, basically this will parse and execute the input passed as a command line argument for eg. ParseCommandLine | dir | sort -desc length | select -first 3 In above example ParseCommandline shud execute the dir then sort the data in a descending order and finally select first 3 rows. How can i programmatically access and execute this commands under ProcessRecord method. Tks for your help. |
My System Specs![]() |
| | #2 (permalink) |
| Guest | Re: Parsing command line arguments In this case, your not passing any parameters to your cmdlet. You cmdlet is just part of the pipeline and its output with be passed to next cmdlet in pipeline (i.e. dir). If you want to execute that script from inside your cmdlet for some reason then pass it as a scriptblock or string (i.e. mycmd {dir|dothis|dothat} ) -- William Stacey [C# MVP] Crypt your script www.powerlocker.com "Yogesh S" <YogeshS@discussions.microsoft.com> wrote in message news:74D48EB8-74FB-46A2-8A40-1308E295A62E@microsoft.com... |I m pretty new to powershell and exploring its capabilities. One of the | things i m trying to experiment is creating a custom cmdlets. This is what i | m thinking of so please let me know if it is possible. | | I have a custom cmdlet ParseCommandline, basically this will parse and | execute the input passed as a command line argument for eg. | | ParseCommandLine | dir | sort -desc length | select -first 3 | | In above example ParseCommandline shud execute the dir then sort the data in | a descending order and finally select first 3 rows. How can i | programmatically access and execute this commands under ProcessRecord method. | | Tks for your help. | | |
My System Specs![]() |
| | #3 (permalink) |
| Guest | Re: Parsing command line arguments "William Stacey [C# MVP]" <william.stacey@gmail.com> wrote in message news:uWWZnimoHHA.5032@TK2MSFTNGP02.phx.gbl... > In this case, your not passing any parameters to your cmdlet. You cmdlet > is > just part of the pipeline and its output with be passed to next cmdlet in > pipeline (i.e. dir). If you want to execute that script from inside your > cmdlet for some reason then pass it as a scriptblock or string (i.e. mycmd > {dir|dothis|dothat} ) You should also be aware that one of the values of the PowerShell environment is that it provides the command parsing facility for each cmdlet. This provides the benefit of consistent approach to argument delimiters, support for both named and positional parameters and partial name matching for named parameters. When creating a cmdlet, all you need to do is declare properties that correspond to your parameters and decorate them with the appropriate attributes e.g.: [AllowNull] [Parameter(Position = 0, Mandatory=true, ValueFromPipeline = true, HelpMessage = "Accepts an object as input to the cmdlet. Enter a variable that contains the objects or type a command or expression that gets the objects.")] public PSObject InputObject { get { return _input; } set { _input = value; } } If you want to take a look at the source of a number of implemented cmdlets download the source for the PowerShell Community Extensions: http://www.codeplex.com/PowerShellCX...leCommits.aspx -- Keith |
My System Specs![]() |
| | #4 (permalink) |
| Guest | Re: Parsing command line arguments Thanks Keith. I assume you wanted to reply to OP. On a related front, it would be cool if partial name matching and wildcards worked for enum names as well. -- William Stacey [C# MVP] Crypt your script www.powerlocker.com "Keith Hill [MVP]" <r_keith_hill@mailhot.nospamIdotcom> wrote in message news:BF5F8974-CCF3-48DE-A885-03110C49675B@microsoft.com... | "William Stacey [C# MVP]" <william.stacey@gmail.com> wrote in message | news:uWWZnimoHHA.5032@TK2MSFTNGP02.phx.gbl... | > In this case, your not passing any parameters to your cmdlet. You cmdlet | > is | > just part of the pipeline and its output with be passed to next cmdlet in | > pipeline (i.e. dir). If you want to execute that script from inside your | > cmdlet for some reason then pass it as a scriptblock or string (i.e. mycmd | > {dir|dothis|dothat} ) | | You should also be aware that one of the values of the PowerShell | environment is that it provides the command parsing facility for each | cmdlet. This provides the benefit of consistent approach to argument | delimiters, support for both named and positional parameters and partial | name matching for named parameters. When creating a cmdlet, all you need to | do is declare properties that correspond to your parameters and decorate | them with the appropriate attributes e.g.: | | [AllowNull] | [Parameter(Position = 0, Mandatory=true, ValueFromPipeline = true, | HelpMessage = "Accepts an object as input to the cmdlet. Enter a | variable that contains the objects or type a command or expression that gets | the objects.")] | public PSObject InputObject | { | get { return _input; } | set { _input = value; } | } | | If you want to take a look at the source of a number of implemented cmdlets | download the source for the PowerShell Community Extensions: | | http://www.codeplex.com/PowerShellCX...leCommits.aspx | | -- | Keith | |
My System Specs![]() |
| | #5 (permalink) |
| Guest | Re: Parsing command line arguments "William Stacey [C# MVP]" <william.stacey@gmail.com> wrote in message news:%23KTMLKnoHHA.4120@TK2MSFTNGP06.phx.gbl... > Thanks Keith. I assume you wanted to reply to OP. Yes, I was just trying to build on what you had said. > On a related front, it > would be cool if partial name matching and wildcards worked for enum names > as well. Partial name matching would be nice but I'm not sure how wildcards for enums would work 'cept maybe for flags style enums. -- Keith |
My System Specs![]() |
| | #6 (permalink) |
| Guest | Re: Parsing command line arguments Just as a lazy helper to disambig. For example mycmd -color "bl???" #black mycmd -color "r?d" #red mycmd -color "g" # Error: gray or green mycmd -color "g*n" # green -- William Stacey [C# MVP] PowerPad, PowerLocker www.powerlocker.com "Keith Hill [MVP]" <r_keith_hill@mailhot.nospamIdotcom> wrote in message news F87E0C7-6279-481E-B2F4-3508DD01221E@microsoft.com...| "William Stacey [C# MVP]" <william.stacey@gmail.com> wrote in message | news:%23KTMLKnoHHA.4120@TK2MSFTNGP06.phx.gbl... | > Thanks Keith. I assume you wanted to reply to OP. | | Yes, I was just trying to build on what you had said. | | > On a related front, it | > would be cool if partial name matching and wildcards worked for enum names | > as well. | | Partial name matching would be nice but I'm not sure how wildcards for enums | would work 'cept maybe for flags style enums. | | -- | Keith | |
My System Specs![]() |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| problem with command line arguments | david | PowerShell | 3 | 02-25-2008 07:29 PM |
| Command line parsing | wkempf | PowerShell | 4 | 02-25-2008 06:57 PM |
| Invalid Command Line arguments??? | Jenna | Vista General | 6 | 09-09-2007 12:33 AM |
| Getting arguments from STDIN when command line arguments are missing | Audun Gjerken | PowerShell | 4 | 03-05-2007 04:01 AM |
| Command Line Arguments | John Smith | PowerShell | 6 | 12-20-2006 08:37 PM |