Reputation: 61463
I have an issue running the following script in a pipeline:
Get-Process | Get-MoreInfo.ps1
The issue is that only the last process of the collection is being displayed. How do I work with all members of the collection in the following script:
param( [Parameter(Mandatory = $true,ValueFromPipeline = $true)]
$Process
)
function Get-Stats($Process)
{
New-Object PSObject -Property @{
Name = $Process.Processname
}
}
Get-Stats($Process)
Upvotes: 1
Views: 282
Reputation: 36708
Actually, both Christian's answer and tbergstedt's answer are both valid--and they are essentially equivalent. You can learn more about how and why in my recent article on Simple-Talk.com: Down the Rabbit Hole- A Study in PowerShell Pipelines, Functions, and Parameters.
In a nutshell, here are the salient points:
Upvotes: 0
Reputation: 3429
I you simply create Get-MoreInfo
as a Filter instead of Function, you will get the desired effect.
Filter Get-MoreInfo
{
param( [Parameter(Mandatory = $true,ValueFromPipeline = $true)]
$Process
)
...
Upvotes: 0
Reputation: 60918
try this:
param( [Parameter(Mandatory = $true,ValueFromPipeline = $true)]
$Process
)
process{
New-Object PSObject -Property @{
Name = $Process.Processname}
}
Edit:
if you need a function:
function Get-MoreInfo {
param( [Parameter(Mandatory = $true,ValueFromPipeline = $true)]
$Process
)
process{
New-Object PSObject -Property @{
Name = $Process.Processname}
}
}
then you can use:
. .\get-moreinfo.ps1 #
Get-Process | Get-MoreInfo
Edit after Comment:
Read about dot sourcing a script
Upvotes: 1