Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to pass -id paramater to get-process from pipeline?

I have a [pscustomobject] with the property ‘id’. According to PowerShell documentation, the parameter ‘-id’ can be passed through the pipeline using ByPropertyName. I discovered that the approach with ForEach-Object works (although I don’t understand why), while the approach without ForEach-Object does not.

$obj = [pscustomobject]@{id=4444}
# doesn't work
$obj | get-process -id $_.id
# it works
$obj | foreach{get-process -id $_.id}

apparently the "foreach" does the trick, but why?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The ValueFromPipelineByPropertyName argument indicates that the parameter accepts input from a property of a pipeline object. The object property must have the same name or alias as the parameter.

The binding happens by pipeline already, you don’t need to manually pass the argument, moreover, $_ (PSItem) is only automatically populated in the context of scriptblock and your second statement has no scriptblock.

$obj = [pscustomobject]@{ id = 4444 }
# works, the value of `id` is taken from pipeline
$obj | Get-Process

As to why the ForEach-Object example works, simply because you’re passing the value of id property as argument to the -Id parameter, it is pretty much the same as:

Get-Process -Id $obj.Id
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading