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 - credential in the pipeline

Reply
 
Old 02-03-2009   #1 (permalink)
MarkTimperley


 
 

credential in the pipeline

I'm writing a cmdlet that can take a PSCredential object from the pipeline
(if needed). This cmdlet does some processing and writes an XmlDocument to
the pipeline. A second cmdlet processes this XmlDocument as a parameter or
from the pipeline. It can also take a PSCredential object from the pipeline
if needed.

I would like it to work like this:

get-myxml | update-myxml

and

get-credential me | get-myxml | update-myxml

(note that I just want the user to provide the credential once, not once for
each cmdlet in the pipeline)

I've been able to pass both a PSCredential and a XmlDocument object in the
pipeline at the same time by adding them both to a PSObject and writting that
to the pipeline. Is this the best way to do this?

I think a side effect of this implementation is that my "by parameter"
interface needs to take a PSObject in order to handle get-myxml output :

$xml = get-myxml
update-myxml $xml

The pipeline interface is able to take "ValueFromPipelineByPropertyName" but
the parameter interface doesn't have this option.

Am I on the right track here? Any suggestions on how to handle the PSObject
as a parameter input? Do I need to switch on type? Thanks in advance...


My System SpecsSystem Spec
Old 02-04-2009   #2 (permalink)
Josh Einstein


 
 

Re: credential in the pipeline

I think by doing it that way you're straying from the more common approach
of storing credential in a variable and passing it as a parameter. It's also
not entirely intuitive to have different objects flowing along the same
pipeline. While some gurus here might be able to help you work out why it
isn't working, it might be worth at least considering if it's the best way.
To me this seems cleaner:

$c = get-credential me
get-myxml -cred $c | update-myxml -cred $c

and if you're using ps 2.0 you can do something like:

$xargs = @{credential=(get-credential me)}
get-myxml @xargs | update-myxml @xargs

finally, to get way off topic, my favorite approach would be a separate
cmdlet altogether:

update-myxml -cred $c -process {
# in this scriptblock $_ has what get-myxml would retrieve
# modify it as needed and at the end it will update
# effectively combining the two cmdlets from before with
# some scriptblock "glue" in the middle
$_.blah = 'new value'
}

Josh

"MarkTimperley" <MarkTimperley@xxxxxx> wrote in message
newsFF7EE84-65D1-4224-BA63-2531DDC9B40A@xxxxxx
Quote:

> I'm writing a cmdlet that can take a PSCredential object from the pipeline
> (if needed). This cmdlet does some processing and writes an XmlDocument to
> the pipeline. A second cmdlet processes this XmlDocument as a parameter or
> from the pipeline. It can also take a PSCredential object from the
> pipeline
> if needed.
>
> I would like it to work like this:
>
> get-myxml | update-myxml
>
> and
>
> get-credential me | get-myxml | update-myxml
>
> (note that I just want the user to provide the credential once, not once
> for
> each cmdlet in the pipeline)
>
> I've been able to pass both a PSCredential and a XmlDocument object in the
> pipeline at the same time by adding them both to a PSObject and writting
> that
> to the pipeline. Is this the best way to do this?
>
> I think a side effect of this implementation is that my "by parameter"
> interface needs to take a PSObject in order to handle get-myxml output :
>
> $xml = get-myxml
> update-myxml $xml
>
> The pipeline interface is able to take "ValueFromPipelineByPropertyName"
> but
> the parameter interface doesn't have this option.
>
> Am I on the right track here? Any suggestions on how to handle the
> PSObject
> as a parameter input? Do I need to switch on type? Thanks in advance...
>
My System SpecsSystem Spec
Old 02-04-2009   #3 (permalink)
MarkTimperley


 
 

Re: credential in the pipeline

The approach you recommend is certainly easier to implement and if it is what
a PowerShell user would normally expect and/or if the use as I outlined is
beyond what the designers of the pipeline intended it for, then I'll go with
your suggestion.

It seems to me that a pipeline that could pass multiple objects between all
cmdlets connected together would be a useful thing.

I would argue that:

get-credential me | get-myxml | update-myxml

is more elegant than:

$c = get-credential me
get-myxml -cred $c | update-myxml -cred $c

but if the former is not common or expected then any perceived elegance on
my part is not worth the effort (since no one will know to use it).

BTW, your "update-myxml -cred $c -process {..." suggestion has me interested
but I can't say I really understand it yet.

"Josh Einstein" wrote:
Quote:

> I think by doing it that way you're straying from the more common approach
> of storing credential in a variable and passing it as a parameter. It's also
> not entirely intuitive to have different objects flowing along the same
> pipeline. While some gurus here might be able to help you work out why it
> isn't working, it might be worth at least considering if it's the best way.
> To me this seems cleaner:
>
> $c = get-credential me
> get-myxml -cred $c | update-myxml -cred $c
>
> and if you're using ps 2.0 you can do something like:
>
> $xargs = @{credential=(get-credential me)}
> get-myxml @xargs | update-myxml @xargs
>
> finally, to get way off topic, my favorite approach would be a separate
> cmdlet altogether:
>
> update-myxml -cred $c -process {
> # in this scriptblock $_ has what get-myxml would retrieve
> # modify it as needed and at the end it will update
> # effectively combining the two cmdlets from before with
> # some scriptblock "glue" in the middle
> $_.blah = 'new value'
> }
>
> Josh
>
> "MarkTimperley" <MarkTimperley@xxxxxx> wrote in message
> newsFF7EE84-65D1-4224-BA63-2531DDC9B40A@xxxxxx
Quote:

> > I'm writing a cmdlet that can take a PSCredential object from the pipeline
> > (if needed). This cmdlet does some processing and writes an XmlDocument to
> > the pipeline. A second cmdlet processes this XmlDocument as a parameter or
> > from the pipeline. It can also take a PSCredential object from the
> > pipeline
> > if needed.
> >
> > I would like it to work like this:
> >
> > get-myxml | update-myxml
> >
> > and
> >
> > get-credential me | get-myxml | update-myxml
> >
> > (note that I just want the user to provide the credential once, not once
> > for
> > each cmdlet in the pipeline)
> >
> > I've been able to pass both a PSCredential and a XmlDocument object in the
> > pipeline at the same time by adding them both to a PSObject and writting
> > that
> > to the pipeline. Is this the best way to do this?
> >
> > I think a side effect of this implementation is that my "by parameter"
> > interface needs to take a PSObject in order to handle get-myxml output :
> >
> > $xml = get-myxml
> > update-myxml $xml
> >
> > The pipeline interface is able to take "ValueFromPipelineByPropertyName"
> > but
> > the parameter interface doesn't have this option.
> >
> > Am I on the right track here? Any suggestions on how to handle the
> > PSObject
> > as a parameter input? Do I need to switch on type? Thanks in advance...
> >
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Credential Issues PowerShell
Can't log in- credential manager General Discussion
credential manager Vista security
get-credential popup PowerShell
get-credential 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