PSThreadJob icon indicating copy to clipboard operation
PSThreadJob copied to clipboard

Remoting question

Open Stephanevg opened this issue 4 years ago • 1 comments

Hi @PaulHigin

I was wondering how this module could help me reduce time of fetching data over the network using Invoke-Command -ComputerName

Is that something that this module could do? would you have an example to share?

Stephanevg avatar Jun 22 '20 13:06 Stephanevg

Invoke-Command already supports fan-out scenarios, so in this case you don't need to use ThreadJob.

$resultsJob = Invoke-Command -Cn @('computer1','computer2','computer3',...) -FilePath .\GatherData.ps1 -ThrottleLimit 25 -AsJob

But if you were just copying files from network shares, or downloading from REST APIs, then because of the slowness of the connection, ThreadJob should speed things up quite a bit.

$destPath = 'c:\DestFolder'
$listOfSourcePaths = 'filePath1','filePath2',...
$listOfSourcePaths | Foreach-Object {
    Start-ThreadJob {
        Copy-Item -Path $using:_ -Dest $using:destPath -Recurse -PassThru -Force
    } -ThrottleLimit 25
} | Wait-Job | Receive-Job

If you are using PowerShell 7+, you can also use the new Foreach-Object -Parallel feature, which is simpler.

$destPath = 'c:\DestFolder'
$listOfSourcePaths = 'filePath1','filePath2',...
$listOfSourcePaths | ForEach-Object -Parallel {
    Copy-Item -Path $_ -Dest $using:destPath -Recurse -PassThru -Force
} -ThrottleLimit 25

PaulHigin avatar Jun 22 '20 20:06 PaulHigin