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

Parsing command line arguments

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 05-29-2007   #1 (permalink)
Yogesh S
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 SpecsSystem Spec
Old 05-29-2007   #2 (permalink)
William Stacey [C# MVP]
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 SpecsSystem Spec
Old 05-29-2007   #3 (permalink)
Keith Hill [MVP]
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 SpecsSystem Spec
Old 05-29-2007   #4 (permalink)
William Stacey [C# MVP]
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 SpecsSystem Spec
Old 05-30-2007   #5 (permalink)
Keith Hill [MVP]
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 SpecsSystem Spec
Old 05-30-2007   #6 (permalink)
William Stacey [C# MVP]
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
newsF87E0C7-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 SpecsSystem Spec
Closed Thread

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


Update your Vista Drivers Update Your Drivers Now!!

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