DOT Source Reference Function in Job Tracker

Ask your PowerShell-related questions, including questions on cmdlet development!
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 6 years and 11 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
toddkenn@gmail.com
Posts: 28
Last visit: Sun Jan 08, 2023 7:26 am

DOT Source Reference Function in Job Tracker

Post by toddkenn@gmail.com »

So I am converting a utility I wrote for Powershell into a GUI app using Forms. Want my form to be responsive "read all the articles" so I decided to use the job-tracker and integrate my code.

The problem is I call a function from within the JobScript by passing in the function and recreating it
  1. $GlobalFuncDef = "function Invoke-Common_ps1 { " + (Get-Command Invoke-Common_ps1).Definition + " }"
  2. $TaskFuncDef = "function $TaskFunction { " + (Get-Command $TaskFunction).Definition + " }"
  3. $JobScript = {
  4.     param (
  5.         [object]$funcdef,
  6.         [string]$funcname,
  7.         [object]$task,
  8.         [object]$feature,
  9.         [object]$GlobalFuncDef
  10.     )
  11.                
  12.     . ([script]::Create($GlobalFuncDef))
  13.     . ([scriptblock]::Create($funcdef))
  14.                        
  15.     & $funcname $task $feature
  16.                        
  17. }
  18. $ArgumentList = $TaskFuncDef, $TaskFunction, $task, $feature, $GlobalFuncDef
I utilize a single PS file for all my common functions which are contained within the $GlobalFuncDef, my problem is that if I DOT source the $GlobalFuncDef I don't get any of the functions defined within it. How can include these in the $JobScript without having to recreate each one? That would be painful.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DOT Source Reference Function in Job Tracker

Post by jvierra »

You really can't pass a function to a script block.

You are up against one ot the issues that plagues all new programmers; you are trying to over-generalize the code.

The best you can do is to pass the function body to the script block and execute it using "Invoke". You can also use the function body as the script block by extracting it into a new script block:
  1. function test {
  2.     Param (
  3.         $msg,
  4.         $color='blue'
  5.     )
  6.     Write-host $msg -fore $color
  7. }
  8. $sb = $function:test.GetNewClosure()
  9. $sb.Invoke('hello world','green')
  10.  
  11. # to use with job
  12. $jobscript = $function:test.GetNewClosure()
  13. $arglist = 'hello world','green'
User avatar
toddkenn@gmail.com
Posts: 28
Last visit: Sun Jan 08, 2023 7:26 am

Re: DOT Source Reference Function in Job Tracker

Post by toddkenn@gmail.com »

I wouldn't call it over generalization, I'm just trying not to duplicate code that's already written. Why is it that I can DOT source a file that contains functions and then use the functions within the scope of the script but when you go out of scope in another thread such as a job you can't DOT source that same file? That doesn't really make sense to me.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DOT Source Reference Function in Job Tracker

Post by jvierra »

You are not dot sourcing a file. The file is embedded in your script. You cannot dot source a function only a file.
User avatar
toddkenn@gmail.com
Posts: 28
Last visit: Sun Jan 08, 2023 7:26 am

Re: DOT Source Reference Function in Job Tracker

Post by toddkenn@gmail.com »

true so some of the code is not correct then, but still poses the problem. Looks like maybe using the job-tracker might not be what I need to use. thanks for the help.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DOT Source Reference Function in Job Tracker

Post by jvierra »

Just leave the functions in a file and dot source the file in the job script.

No matter what method you use you will have the exact same issue. All threads are independent and cannot easily share functions or variables.

You could create a custom class with static methods and pass that to the tracker. An object carries its methods with it.
This topic is 6 years and 11 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