I am trying to start a function with start-job. For some reason, I cannot get it to work, the global variables wont pass. My question would be - how to pass global variables to the function and than to start-job without declaring them inside the function. Function example:
$OutputDirectory=c:\test
$zipfile="c:\1.zip"
function 7zipextraction
{
Set-Alias 7zip $7zip_path
# Extracting files
$extract = 7zip x -y $zipfile -o"$OutputDirectory"
if ($LASTEXITCODE -ne 0)
{
write-host "7zip Error"
return
}
else
{
write-host "Files extracted successfully"
}
Start-Sleep -s 2
}
Examples from start-job:
Start-Job -ScriptBlock ${Function:7zipextraction} | Wait-Job | Receive-Job