How to pass data down the pipeline

Welcome Forums General PowerShell Q&A How to pass data down the pipeline

This topic contains 7 replies, has 4 voices, and was last updated by

 
Participant
1 year, 8 months ago.

  • Author
    Posts
  • #66381

    Participant
    Points: 0
    Rank: Member

    Hi,

    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.

    Example:
    "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

    Keymaster
    Points: 1,638
    Helping HandTeam Member
    Rank: Community Hero

    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

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    Thank you Curtis (that's for you > https://beeroverip.org/ ), 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

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    That would do the job as well, thanks.

  • #66579

    Participant
    Points: 0
    Rank: Member

    without additional variables:

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

    Participant
    Points: 0
    Rank: Member

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

The topic ‘How to pass data down the pipeline’ is closed to new replies.