Search code examples
wpfpowershellmodulejobs

Use script who use functions stocked in another ps1 with Start-job


I'm trying a little WPF interface with powershell for compare two files. For do that, I want start 2 jobs with script who was stocked in "Jobs.ps1"

Param(
    $PathFile1,
    $PathFile2,
    $Json
)

$Hash = CreateHashConverted -Path $PathFile1 -Fichier $Json
$List = [System.Collections.ArrayList]::New()
$List = CompareFile -Path $PathFile2 -Hash $Hash

return $List

But actually, "CreateHashConverted" and "CompareFile" uses somes functions who was stocked in "JsonFunctions.ps1" and "ReadFunctions.ps1"

When i try to use my start job

$null = Start-Job -Name "1vers2" -FilePath "$PSScriptRoot\..\Logique\Jobs.ps1" -ArgumentList $txtb_file1,$txtb_file2,$lb_LISTE.SelectedItem

My job doesn't know CreateHashConverted and CompareFile

Thank's for help


Solution

  • Use the -InitializationScript parameter to import the function definitions needed by the job:

    $null = Start-Job -Name "1vers2" -InitializationScript { . "$PSScriptRoot\..\Logique\JsonFunctions.ps1"; . "$PSScriptRoot\..\Logique\ReadFunctions.ps1" } -FilePath "$PSScriptRoot\..\Logique\Jobs.ps1" -ArgumentList $txtb_file1,$txtb_file2,$lb_LISTE.SelectedItem