Syphirint Syphirint - 3 months ago 29
PowerShell Question

Parallel in Workflow

I'm trying to understand how parallel workflows work, so I started developing a simple script for the job. But cannot understand how to retrieve (or even save) the information.


EDIT : Forgot to mention that the following code doesn't work as I expected.

$wks ='wks1.domain.com','wks2.domain.com','wks3.domain.com'

workflow ExampleWorkflow{
param ($wks_list)

$array_workflow = @()

foreach -Parallel ($workstation in $wks_list){
$processes = Get-Process -PSComputerName $workstation
foreach($process in $processes){
$info = New-Object -TypeName PSObject
$info | Add-Member -Type NoteProperty -Name ID -Value $process.ID
$info | Add-Member -Type NoteProperty -Name ProcessName -Value $process.ProcessName
$info | Add-Member -Type NoteProperty -Name RAM -Value $process.PM
$info | Add-Member -Type NoteProperty -Name WKS -Value $workstation

$WORKFLOW:array_workflow += $info
}
}
}
ExampleWorkflow $wks


Can someone help me understand how to manipulate data inside the workflow so that after that can be accessible after exiting the workflow?

Answer

After fiddling around with the code, with some help from the answers and some research, came up with this solution that worked for me:

$wks ='wks1.domain.com','wks2.domain.com','wks3.domain.com'

workflow ExampleWorkflow{
    param ($wks_list)

    foreach -Parallel ($workstation in $wks_list){
        InlineScript{
            $processes = Get-Process -ComputerName $using:workstation
            foreach($process in $processes){
                $obj = New-Object -type PSObject -Property  @{ 
                    ID = $process.ID
                    ProcessName = $process.ProcessName
                    Memory = ($process.PM / 1MB)
                    Workstation = $using:desktop
                }
                Write-Output $obj
            }
        }
    }
}
$var = ExampleWorkflow $wks
Do-Stuff with $var
Comments