0

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

Aomichi
  • 57
  • 5

1 Answers1

1

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
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Doesn't work, i get this The term "\..\Logic\JsonFunctions.ps1" is not recognized as the name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path exists, verify that the path is correct and try again. This error for Both Scripts – Aomichi Jan 31 '23 at 13:19
  • @Aomichi That would indicate you aren't running this as part of a script. Use `$PWD` in place of `$PSScriptRoot` to interpret paths relative to your current location – Mathias R. Jessen Jan 31 '23 at 13:24
  • I can't use $PWD because i'm working in another Hardrive but with ([System.Environment]::CurrentDirectory) that work Thank's you so much ! – Aomichi Jan 31 '23 at 13:32