![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 http://msdn2.microsoft.com/en-us/lib...mmandbase.aspx Example: Quote: >"test"|set-content test.tmp >get-content test.tmp Quote: >remove-item test.tmp >"test"|set-content test.tmp|out-host <--nothing to console >get-content test.tmp Quote: > >"test"|set-content test.tmp -passthru|out-host Quote: >get-content test.tmp Marco -- Microsoft MVP - Windows PowerShell http://www.microsoft.com/mvp PowerGadgets MVP http://www.powergadgets.com/mvp Blog: http://marcoshaw.blogspot.com |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Developing Cmdlet Quote: > Example: > Quote: > >"test"|set-content test.tmp > >get-content test.tmp Quote: > >remove-item test.tmp > >"test"|set-content test.tmp|out-host <--nothing to console > >get-content test.tmp Quote: > > commands before sending the post. Quote: Quote: > >"test"|set-content test.tmp -passthru|out-host Quote: > >get-content test.tmp |
My System Specs![]() |
| | #4 (permalink) |
| | 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 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 Specs![]() |
| | #5 (permalink) |
| | 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. 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. 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 Specs![]() |
| | #6 (permalink) |
| | 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 Quote: > >remove-item test.tmp > >"test"|set-content test.tmp|out-host <--nothing to console > >get-content test.tmp Quote: > > > >"test"|set-content test.tmp -passthru|out-host Quote: > >get-content test.tmp > > Marco > > -- > Microsoft MVP - Windows PowerShell > http://www.microsoft.com/mvp > > PowerGadgets MVP > http://www.powergadgets.com/mvp > > Blog: > http://marcoshaw.blogspot.com > |
My System Specs![]() |
| | #7 (permalink) |
| | 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."? 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 Specs![]() |
| | #8 (permalink) |
| | 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 Quote: > >remove-item test.tmp > >"test"|set-content test.tmp|out-host <--nothing to console > >get-content test.tmp Quote: > > > >"test"|set-content test.tmp -passthru|out-host Quote: > >get-content test.tmp > > Marco > > -- > Microsoft MVP - Windows PowerShell > http://www.microsoft.com/mvp > > PowerGadgets MVP > http://www.powergadgets.com/mvp > > Blog: > http://marcoshaw.blogspot.com > |
My System Specs![]() |
![]() |
| 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 | |||