JobTracker - How to run a user defined function in the JobScript

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
kvinther
Posts: 1
Last visit: Wed Jan 18, 2017 5:19 am

JobTracker - How to run a user defined function in the JobScript

Post by kvinther »

I am using the Job Tracker framework provided as a download by SAPIEN.

My question is, how do I run "one of my own" functions from the JobScript script block?

Below is a simple example of what I am trying to achieve (in my real example my function does a lot more):
### global.ps1 ###
function Get-ScriptDirectory { ... }

### MainForm.psf ###
$button_click = {
 Add-JobTracker -Name "Job1" -JobScript { Get-ScriptDirectory } -CompletedScript { /* Receive-Job and update a text box*/ }
}
However, when I run this I get an exception saying: "The term 'Get-ScriptDirectory' is not recognized as the name of a cmdlet, function, script file or operable program."

It seems that my global functions are not in scope in the JobScript script block. However, if I call the Get-ScriptDirectory function directly from the CompletedScript, it works just fine. I tried passing my function as a parameter to the JobScript, but that still gave me the exception. If I pass a JobScript with either {ls function:} or {get-command *get-script*}, I get the function/command lists, by they only contain the standard PowerShell session functions/commands.

So how to I get my functions to be in-scope in the JobScript?

Note: I've found another post on this forum regarding the same issue, but no actual solution was found here: viewtopic.php?f=21&t=9384&p=50858&hilit ... ion#p50858

Any input will be much appreciated!

Kristian
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: JobTracker - How to run a user defined function in the JobScript

Post by jvierra »

Any function referenced in a any PowerShell "Job" must be included in the job's script block. This is a fundamental requirement of PowerShell and hot PSS or the JobTracker control set.

You can pass values into the scriptblock.
  1. $scriptDir = Get-ScriptDirectory
  2. Add-JobTracker -Name Job1 -JobScript {
  3.         Param(
  4.             $ScriptDirectory
  5.         )
  6.         # other code
  7.         } -CompletedScript {
  8.             /* Receive-Job and update a text box*/
  9.         } -ArgumentList $scriptdir
This topic is 7 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked