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 - Developing Cmdlet

Reply
 
Old 12-11-2007   #1 (permalink)
Eric Quist


 
 

Developing Cmdlet

Hello

I'm rather new to PowerShell and I'm a developer with limited experience of
PowerShell and other shells like it. I have started to create som Cmdlet's
for administrating an application that I'm building. The Cmdlets is there to
ease the modification of the .NET web.config file.

So I have a Cmdlet that opens the config-file called Get-ConfigurationFile
and I then have a couple of other Cmdlet's that modifies different parts of
the config file. So long so good.

My problem is that there is a hierarchy in the config file with service and
then providers for each service. On the service there are more attributes
that could be set so my thought was to create a Cmdlet that could get the
Service and another Cmdlet that could get the providers. As input fot the
cmdlet handling the provider I need both the configuration file and the
service configuration element, so that cmdlet has two parameter properties
that are specified to get their values from the pipe. Now my problem is that
the configuration property is not set, when writing for example this command
in PS.

Get-ConfigurationFile "/TestService" | Get-Service "Service1" |
Get-ServiceProviders

I have two questions:
1. Results that are written too the pipe is only available to the next
Cmdlet? In this case Get-Service, but not Get-ServiceProviders.
2. If "yes" is the answer two the first question, then I wonder if I have
some fundamently wrong thinking in my split of Cmdlet. Is it not a good idea
to have a Cmdlet for opening the configuration, and then a lot of other
commands operating on the opened configuration? Should each Cmdlet be
responsible for opening the Cmdlet? If not ... is there any way to push the
opened configuration futher down the pipe? I would rather not use WriteObject
with the configuration in Get-Service again, because then it will clutter the
output when the command stops with Get-Service (no call to
Get-ServiceProviders).

Sorry for the long post, but I found it difficult to explain. Thanks, Eric.

My System SpecsSystem Spec
Old 12-11-2007   #2 (permalink)
Marco Shaw [MVP]


 
 

Re: Developing Cmdlet

Eric Quist wrote:
Quote:

> Hello
>
> I'm rather new to PowerShell and I'm a developer with limited experience of
> PowerShell and other shells like it. I have started to create som Cmdlet's
> for administrating an application that I'm building. The Cmdlets is there to
> ease the modification of the .NET web.config file.
>
> So I have a Cmdlet that opens the config-file called Get-ConfigurationFile
> and I then have a couple of other Cmdlet's that modifies different parts of
> the config file. So long so good.
>
> My problem is that there is a hierarchy in the config file with service and
> then providers for each service. On the service there are more attributes
> that could be set so my thought was to create a Cmdlet that could get the
> Service and another Cmdlet that could get the providers. As input fot the
> cmdlet handling the provider I need both the configuration file and the
> service configuration element, so that cmdlet has two parameter properties
> that are specified to get their values from the pipe. Now my problem is that
> the configuration property is not set, when writing for example this command
> in PS.
>
> Get-ConfigurationFile "/TestService" | Get-Service "Service1" |
> Get-ServiceProviders
Lookup "passthru". Not all base cmdlets support it.

http://msdn2.microsoft.com/en-us/lib...mmandbase.aspx

Example:
Quote:

>"test"|set-content test.tmp
>get-content test.tmp
test
Quote:

>remove-item test.tmp
>"test"|set-content test.tmp|out-host <--nothing to console
>get-content test.tmp
test
Quote:

>
>"test"|set-content test.tmp -passthru|out-host
test <--console output
Quote:

>get-content test.tmp
test

Marco

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
My System SpecsSystem Spec
Old 12-11-2007   #3 (permalink)
Marco Shaw [MVP]


 
 

Re: Developing Cmdlet

Quote:

> Example:
>
Quote:

> >"test"|set-content test.tmp
> >get-content test.tmp
> test
Quote:

> >remove-item test.tmp
> >"test"|set-content test.tmp|out-host <--nothing to console
> >get-content test.tmp
> test
Quote:

> >
I did actually do a remove-item here, but lost it when I reorganized my
commands before sending the post.
Quote:
Quote:

> >"test"|set-content test.tmp -passthru|out-host
> test <--console output
Quote:

> >get-content test.tmp
> test
My System SpecsSystem Spec
Old 12-11-2007   #4 (permalink)
Marco Shaw [MVP]


 
 

Re: Developing Cmdlet

Quote:
Quote:

>> Get-ConfigurationFile "/TestService" | Get-Service "Service1" |
>> Get-ServiceProviders
>
> Lookup "passthru". Not all base cmdlets support it.
>
> http://msdn2.microsoft.com/en-us/lib...mmandbase.aspx
OK, I looked too quickly and assumed you were using all your own
cmdlets. You are using the base 'get-service'? That get-service does
not provide a -passthru parameter.

You migth be able to substitute a script or scriptblock in the pipeline
to conserve what you might need.

Can you give a full example and what get-serviceproviders expects?

Marco
My System SpecsSystem Spec
Old 12-11-2007   #5 (permalink)
Marco Shaw [MVP]


 
 

Re: Developing Cmdlet

Quote:

> When Get-Service gets called (as in my first example) the Configuration
> property is set, but when Get-ServiceProvider gets called it's not set,
> resulting in an exception since it's mandatory.
>
> I would like the Configuration property to be set in the last cmdlet in the
> pipe to, not just for the first one.
And to do that, you actually could use the -passthru parameter as I
first indicated. I'll have to see if I can find time to come up with an
example. Likely someone else will beat me to the punch.
Quote:

>
> I think I read somewhere that you should derive your classes from Cmdlet,
> rather then PSCmdlet if you want to be able (at least easier) to use the
> Cmdlet's as the foundation that a visual MMC admin snap-in should use.
Well, calling your cmdlet should be easier if you derive from cmdlet
when you come to invoking your cmdlets later in C#. For example:

http://marcoshaw.blogspot.com/2007/0...cmdlet_25.html
http://marcoshaw.blogspot.com/2007/0...cmdlet_24.html

Marco
My System SpecsSystem Spec
Old 12-11-2007   #6 (permalink)
Eric Quist


 
 

Re: Developing Cmdlet

What about the documentation stating: "This class cannot be used directly,
nor should it be used to derive other classes."?

Thanks, Eric

"Marco Shaw [MVP]" wrote:
Quote:

> Eric Quist wrote:
Quote:

> > Hello
> >
> > I'm rather new to PowerShell and I'm a developer with limited experience of
> > PowerShell and other shells like it. I have started to create som Cmdlet's
> > for administrating an application that I'm building. The Cmdlets is there to
> > ease the modification of the .NET web.config file.
> >
> > So I have a Cmdlet that opens the config-file called Get-ConfigurationFile
> > and I then have a couple of other Cmdlet's that modifies different parts of
> > the config file. So long so good.
> >
> > My problem is that there is a hierarchy in the config file with service and
> > then providers for each service. On the service there are more attributes
> > that could be set so my thought was to create a Cmdlet that could get the
> > Service and another Cmdlet that could get the providers. As input fot the
> > cmdlet handling the provider I need both the configuration file and the
> > service configuration element, so that cmdlet has two parameter properties
> > that are specified to get their values from the pipe. Now my problem is that
> > the configuration property is not set, when writing for example this command
> > in PS.
> >
> > Get-ConfigurationFile "/TestService" | Get-Service "Service1" |
> > Get-ServiceProviders
>
> Lookup "passthru". Not all base cmdlets support it.
>
> http://msdn2.microsoft.com/en-us/lib...mmandbase.aspx
>
> Example:
>
Quote:

> >"test"|set-content test.tmp
> >get-content test.tmp
> test
Quote:

> >remove-item test.tmp
> >"test"|set-content test.tmp|out-host <--nothing to console
> >get-content test.tmp
> test
Quote:

> >
> >"test"|set-content test.tmp -passthru|out-host
> test <--console output
Quote:

> >get-content test.tmp
> test
>
> Marco
>
> --
> Microsoft MVP - Windows PowerShell
> http://www.microsoft.com/mvp
>
> PowerGadgets MVP
> http://www.powergadgets.com/mvp
>
> Blog:
> http://marcoshaw.blogspot.com
>
My System SpecsSystem Spec
Old 12-11-2007   #7 (permalink)
Marco Shaw [MVP]


 
 

Re: Developing Cmdlet

Eric Quist wrote:
Quote:

> What about the documentation stating: "This class cannot be used directly,
> nor should it be used to derive other classes."?
Oh, RTFM?!

Well, that's how it would work. Otherwise, it looks like you will have
to adapt your cmdlet to take the object in, and then reoutput it again.
If that makes any sense...

A script or scriptblock could also do this likely. The v2 CTP could
even hide it somewhat with a scriptcmdlet.

Marco
My System SpecsSystem Spec
Old 12-13-2007   #8 (permalink)
Eric Quist


 
 

Re: Developing Cmdlet

How would you design cmdlets that should operate on config-files? Would you
go for the approach where you pipe the configuration (my initial try) or
would you let the configuration file be a property on each cmdlet?

Thanks in advance, Eric

"Marco Shaw [MVP]" wrote:
Quote:

> Eric Quist wrote:
Quote:

> > Hello
> >
> > I'm rather new to PowerShell and I'm a developer with limited experience of
> > PowerShell and other shells like it. I have started to create som Cmdlet's
> > for administrating an application that I'm building. The Cmdlets is there to
> > ease the modification of the .NET web.config file.
> >
> > So I have a Cmdlet that opens the config-file called Get-ConfigurationFile
> > and I then have a couple of other Cmdlet's that modifies different parts of
> > the config file. So long so good.
> >
> > My problem is that there is a hierarchy in the config file with service and
> > then providers for each service. On the service there are more attributes
> > that could be set so my thought was to create a Cmdlet that could get the
> > Service and another Cmdlet that could get the providers. As input fot the
> > cmdlet handling the provider I need both the configuration file and the
> > service configuration element, so that cmdlet has two parameter properties
> > that are specified to get their values from the pipe. Now my problem is that
> > the configuration property is not set, when writing for example this command
> > in PS.
> >
> > Get-ConfigurationFile "/TestService" | Get-Service "Service1" |
> > Get-ServiceProviders
>
> Lookup "passthru". Not all base cmdlets support it.
>
> http://msdn2.microsoft.com/en-us/lib...mmandbase.aspx
>
> Example:
>
Quote:

> >"test"|set-content test.tmp
> >get-content test.tmp
> test
Quote:

> >remove-item test.tmp
> >"test"|set-content test.tmp|out-host <--nothing to console
> >get-content test.tmp
> test
Quote:

> >
> >"test"|set-content test.tmp -passthru|out-host
> test <--console output
Quote:

> >get-content test.tmp
> test
>
> Marco
>
> --
> Microsoft MVP - Windows PowerShell
> http://www.microsoft.com/mvp
>
> PowerGadgets MVP
> http://www.powergadgets.com/mvp
>
> Blog:
> http://marcoshaw.blogspot.com
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Quest AD cmdlet -what is the cmdlet to remove computer object PowerShell
Invoking Cmdlet Get-Location from cmdlet,cant get Currnt Directory PowerShell
Developing a cmdlet to show constant name rather than value PowerShell
Whether a cmdlet derives from cmdlet or pscmdlet PowerShell


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