Niniki
Niniki

Reputation: 810

How do I pass common powershell command line parameters between cmdlets on the pipeline?

Say I've got two cmdlets, 'new-foo' and 'do-bar'. Both cmdlets need to authenticate to a service in order to perform their action, and 'do-bar' takes a foo. Today, I can do:

new-foo -host localhost -username user -password password -whateverOtherArgs

And I can do:

do-bar -host localhost -username user -password password -foo myFoo

And I can even chain them passing foo on the pipeline, e.g.:

new-foo <blah blah> | do-bar -host localhost -username user -password password

But I can't figure out how to pass the common parameters, such as the service location and the credentials between elements of the pipeline. If I've got a bunch of my cmdlets chained together I'd like to only pass the credentials the first time, and then re-use those for the rest of the pipeline.

What am I missing, seems like this should be obvious ...

Upvotes: 2

Views: 1511

Answers (3)

jamesdeath123
jamesdeath123

Reputation: 4606

Are you developing this cmdlets, or are you just using them in your scripts? If you are writing these cmdlets in any OOP language, I think a way to do it is that you have a base cmdlet class, and every other cmdlets should extend that base cmdlet class. In this case, the base cmdlet class should initialize an object that stores your credential, and have a cmdlet to ask for credentials, which initialize the object. Every other cmdlets that extends the base class can therefore look for credential in this object.

Upvotes: 0

Keith Hill
Keith Hill

Reputation: 201652

You could have New-Foo spit out an object that contains both the original object that do-bar is interested in as well as the service location and the credentials as properties. Accept this object as a parameter and then pluck out the data you need if the user doesn't supply the ServiceLocation or Credential parameters.

Upvotes: 1

rerun
rerun

Reputation: 25505

does Get-Credential cover you need.

Upvotes: 0

Related Questions