Powershell start-job -scriptblock cannot recognize

2020-06-10 03:40发布

I have the following code.

function createZip
{
Param ([String]$source, [String]$zipfile)
Process { echo "zip: $source`n     --> $zipfile" }
}

try {
    Start-Job -ScriptBlock { createZip "abd" "acd" } 
}
catch {
    $_ | fl * -force
}
Get-Job | Wait-Job 
Get-Job | receive-job 
Get-Job | Remove-Job 

However, the script returns the following error.

Id              Name            State      HasMoreData     Location             Command                  
--              ----            -----      -----------     --------             -------                  
309             Job309          Running    True            localhost            createZip "a...
309             Job309          Failed     False           localhost            createZip "a...
Receive-Job : The term 'createZip' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:17 char:22
+ Get-Job | receive-job <<<<  
    + CategoryInfo          : ObjectNotFound: (function:createZip:String) [Receive-Job], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

It seems the function name cannot be recognized inside the script block of start-job. I tried function:createZip too.

2条回答
地球回转人心会变
2楼-- · 2020-06-10 04:20

If you are still new to using start-job and receive-job, and want to debug your function more easily, try this form:

   $createZip = { 
   function createzipFunc {
     param ([String]$source, [String]$zipfile)
     Process { echo "zip: $source`n     --> $zipfile" }
     }
     #other funcs and constants here if wanted...
   }
   # some secret sauce, this defines the function(s) for us as locals
   invoke-expression $createzip

   #now test it out without any job plumbing to confuse you
   createzipFunc "abd" "acd"

   # once debugged, unfortunately this makes calling the function from the job  
   # slightly harder, but here goes...
   Start-Job -initializationScript $createZip -scriptblock {param($a,$b) `
createzipFunc $a $b } -ArgumentList "abc","def"

All not made simpler by the fact I did not define my function as a simple filter as you have, but which I did because I wanted to pass a number of functions into my Job in the end.

Sorry for digging this thread out, but it solved my problem too and so elegantly at that. And so I just had to add this little bit of sauce which I had written while debugging my powershell job.

查看更多
Anthone
3楼-- · 2020-06-10 04:29

Start-Job actually spins up another instance of PowerShell.exe which doesn't have your createZip function. You need to include it all in a script block:

$createZip = {
    param ([String]$source, [String]$zipfile)
    Process { echo "zip: $source`n     --> $zipfile" }
}

Start-Job -ScriptBlock $createZip  -ArgumentList "abd", "acd"

An example returning an error message from the background job:

$createZip = {
    param ([String] $source, [String] $zipfile)

    $output = & zip.exe $source $zipfile 2>&1
    if ($LASTEXITCODE -ne 0) {
        throw $output
    }
}

$job = Start-Job -ScriptBlock $createZip -ArgumentList "abd", "acd"
$job | Wait-Job | Receive-Job

Also note that by using a throw the job object State will be "Failed" so you can get only the jobs which failed: Get-Job -State Failed.

查看更多
登录 后发表回答