I need to use the output from first function to the second function. The output from the first function would be generated little by little but I would expect the 2nd function to consume it immediately when it's available. Can you please help? I use sleep 5 to demonstrate the delay. Below is the code.
Function Test-Service {
$p = Get-Service | select name, CanStop
foreach($m in $p)
{
$instance = New-Object PSObject -Property @{
Name = $m.name
CanStop = $m.CanStop
}
write-host $instance
sleep 5
}
}
Function Get-Something {
[CmdletBinding()]
Param(
[Parameter(ValueFromPipelineByPropertyName)]
$instance
)
process {
$name = $instance.name
$canstop = $instance.canstop
Write-Host "You passed the parameter $Name $CanStop into the function"
}
}
The output is as below:
@{Name=AarSvc_f5f1f38; CanStop=True} @{Name=AdobeARMservice; CanStop=True} ... which is the output of the first function "Test-Service". It seems it didn't even received by the Get-Something
Thanks
Update #1: After check @mklement0 and @zett42's feedback I realized I misunderstood the Write-Host vs pipleline.
I then tried a new example as below(Which is more of my real world code) I created a lot of empty .tbd files under current directory according to the $list. I then read the list, get the x and y position and pass the name, x, y to next function to consume The way I ran them is:
Populate-NextIcon -List $instance| Launch-Test
I would expect to see the output from screen periodically but there's no output at all. which means my "Launch-Test" function was not invoked properly.
function Launch-TEST
{
[CmdletBinding()]
Param(
[Parameter(ValueFromPipelineByPropertyName )]
[String]$name,
[Parameter(ValueFromPipelineByPropertyName )]
[int]$x,
[Parameter(ValueFromPipelineByPropertyName )]
[int]$y
)
$prefix = "Launch-"
$message = "${prefix}: Received ${name}"
write-host $message
}
function Populate-NextIcon
{
param ([System.Collections.ArrayList]$list)
while($true)
{
$icons = Get-ChildItem -path .\*.tbd | Sort-Object LastWriteTime
if($icons.count -eq 0)
{
break
}
$t = Get-Date -Format "yyyy_MM_dd_HHmmss"
$icon = $icons[0]
$baseName = $icon.BaseName
Add-Content -Path $icon.FullName -Value $t
$i = $list | where-object {$_.name -eq $baseName}
[PSCustomObject]@{
Name = $i.name
x = $i.x
y = $i.y
}
$sleep = (Get-Content -Path $icon.FullName | Measure-Object -Line).Lines
$sleep = $sleep * 5
sleep $sleep
}
}
If I ran it as Populate-NextIcon -List $list
I will get below output as expected:
Name x y
---- - -
AAA_PROD 1300 358
BBB_PROD 2068 250
CCC_PROD 1556 358
But if i pipelined it as Populate-NextIcon -List $instance| Launch-Test
Then although i can see these .tbd files got updated there's no output in the screen It seems the output from first function is held up somewhere?