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
CodePudding user response:
Start-Job dispatches the job to a separate child process, which is why it doesn't have access to the variables you've defined in the calling process.
Parameterize all variables in the function and then pass the appropriate arguments explicitly to Start-Job -ArgumentList:
function 7zipextraction
{
param(
[string]$ZipFile,
[string]$OutputDirectory,
[string]$7zipPath
)
Set-Alias 7zip $7zipPath -Force
# 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
}
Start-Job -ScriptBlock ${Function:7zipextraction} -ArgumentList "c:\1.zip","c:\test","c:\path\to\7zip\7z.exe" | Wait-Job | Receive-Job
CodePudding user response:
You can use the using: modifier to access variables that are outside your script block:
$OutputDirectory='c:\test'
$zipfile='c:\1.zip'
$7zippath = 'C:\path\to\7zip'
function 7zipextraction
{
Set-Alias 7zip $using:7zippath
# Extracting files
$extract = 7zip x -y $using:zipfile -o"${using:OutputDirectory}"
if ($LASTEXITCODE -ne 0)
{
write-host "7zip Error"
return
}
else
{
write-host "Files extracted successfully"
}
Start-Sleep -s 2
}
Start-Job -ScriptBlock ${Function:7zipextraction} | Receive-Job -Wait -AutoRemoveJob
using works with script blocks executed on remote computers or in a different thread:
- Invoke-Command
- Start-Job
- Start-ThreadJob
- ForEach-Object -Parallel
