Author Posts

March 14, 2017 at 3:16 pm


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.

March 14, 2017 at 3:20 pm

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?

March 15, 2017 at 4:12 am

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}}, *

March 15, 2017 at 12:01 pm

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).

March 15, 2017 at 5:22 pm

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}}, *

March 15, 2017 at 5:47 pm

That would do the job as well, thanks.

March 16, 2017 at 10:20 am

without additional variables:

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

March 16, 2017 at 2:42 pm

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