How to pass data down the pipeline

This topic contains 7 replies, has 4 voices, and was last updated by  Curtis Smith 1 year, 2 months ago.

  • Author
  • #66381



    I would like to create one liner where I can (somehow) pass computer name down the pipeline.
    This will be useful when I have a list with computers that will return me value.

    "Get-Service -computername localhost" will return
    Status Name DisplayName
    —— —- ———–

    What I'm aiming is to have one more column:
    Computer Status Name DisplayName
    ——– —— —- ———–

    Can you advice me, how to get that?
    I've tied with -pipelinevariable parameter, but didn't get any success.

  • #66384

    Don Jones

    That's difficult in a one-liner, because the object produced by Get-Service doesn't contain that information. Ergo, it doesn't exist in the pipeline. You can't "pass data through a command;" the pipeline can only contain what the most recent command ran.

    You can accomplish what you want, but not with a one-liner. Why is a one-liner a fixed goal for you?

  • #66406

    Curtis Smith

    Fun challenge. Here's what I came up with.

    Get-Service -computername ((Set-Variable -name "comp" -value "localhost" -PassThru).value) | Select-Object @{Label = 'ComputerName'; Expression = {$comp}}, *
  • #66436


    Thank you Curtis (that's for you > ), however I now realize that what I want is not possible as the data do not exist.

    I wanted to run is the command bellow:
    Get-VirtualSwitch -VMHost (get-content c:\hosts.txt) | where {name -like 'Cluster1_*'}

    It will return only names of the filtered Switch, but not which host have it. So nothing major, but still not something that should deserves to be write in ISE (which will load the vmware snapin/module for a lot of time).

  • #66499

    Curtis Smith

    Hey @miroslavvasilev,
    While I don't really think it's worth typing, the same concept can still be used

    get-content 'D:\Temp\computers.txt' | ForEach-Object {Get-Service -ComputerName ((Set-Variable -name "comp" -value $_ -PassThru).value)} | Select-Object @{Label = 'ComputerName'; Expression = {$comp}}, *

    Or in your case

    get-content 'D:\Temp\computers.txt' | ForEach-Object {Get-VirtualSwitch -VMHost ((Set-Variable -name "vmhost" -value $_ -PassThru).value)} | where {name -like 'Cluster1_*'} | Select-Object @{Label = 'ComputerName'; Expression = {$vmhost}}, *
  • #66504


    That would do the job as well, thanks.

  • #66579

    Max Kozlov

    without additional variables:

    get-content 'D:\Temp\computers.txt' | ForEach-Object { Get-Service -ComputerName $_ | Add-Member -MemberType NoteProperty -Name ComputerName -Value $_ -PassThru }
  • #66618

    Curtis Smith

    Ooo, that's nice @max-kozlov, good job.

You must be logged in to reply to this topic.