returning of an object from jobtracker completedscript block

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 8 years and 4 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
noescape
Posts: 16
Last visit: Tue Jan 04, 2022 2:17 am

returning of an object from jobtracker completedscript block

Post by noescape »

Hi,

I'm struggling with returning of an object from a finished job from inside the jobtracker framework.

The -jobscript{} block processes and returns some data that are stored in a custom object, then I pass the data to -completedscript{} block with
  1. return $data
at the end of the jobscript.
In the
  1. -completedscript{}
block I use receive-job to retrieve the data:
  1. -CompletedScript {
  2.                 Param ($Job)
  3.                 $data = @{ }
  4.                 $data = Receive-Job -Job $Job
  5. }
That's all fine, the data is stored in $data variable but gets lost just when the -completedscript{} block finishes and leaves the jobtracker function. My question is, how do I store / pass the data from the block so it doesn't get lost when the jobtracker function ends? I was thinking that it has something to do with the Update-Jobtracker function that calls the scriptblock via invoke-command. Can you please help / guide me to better understanding of this framework? I read all about 'creating responsive forms' but couldn't find anything helpful.

Thanks in advance,

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

Re: returning of an object from jobtracker completedscript block

Post by jvierra »

Where do you want t "store" the data?
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: returning of an object from jobtracker completedscript block

Post by dan.potter »

Easiest way is a psobject. Merely assigning variables does not constitute data. You can see the difference in the shell. get-job will show if the job has data or not.

I posted code yesterday on jobtracker in the PSGUI topic.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: returning of an object from jobtracker completedscript block

Post by jvierra »

Dan - the likelihood that the result is a PsObject already...

Just creating an object does not "store" anything.

We could output to a file. We could export to a CSV file. We could display in a textbox.
What does "store" mean? As used here, "store" is an ambiguous term and not a technical term.
User avatar
noescape
Posts: 16
Last visit: Tue Jan 04, 2022 2:17 am

Re: returning of an object from jobtracker completedscript block

Post by noescape »

I want to have access to the data outside of the jobtracker function but not output it to a file. Basically what I am trying to achieve is having a Job#1 return some data and save it outside of the job when it finishes. Then I need to run Job#2 which will also create very similar data and after it finishes I want to compare those results and out-file them to a text file.

The way I tried to achieve that was creating an object inside the scriptblock itself, then I can return it to "completed script" block but I'm not able to get it "outside" of the function after that. Think it's because the "completed script" gets processed in the "Update-Jobtracker" function with Invoke-Command,but the output doesn't get saved so that I could see it from outside after that. I don't want to mess with the jobtracker framework so maybe there is another approach I could use.

Tom
User avatar
noescape
Posts: 16
Last visit: Tue Jan 04, 2022 2:17 am

Re: returning of an object from jobtracker completedscript block

Post by noescape »

I wanted to provide a code I'm working with but anti-spam is blocking me from posting it (URL links are not allowed - there aren't any links in it tho).
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: returning of an object from jobtracker completedscript block

Post by jvierra »

noescape wrote:I wanted to provide a code I'm working with but anti-spam is blocking me from posting it (URL links are not allowed - there aren't any links in it tho).
To post links you need to use the add attachments or use the full editor and not the "quick" edit box.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: returning of an object from jobtracker completedscript block

Post by jvierra »

noescape wrote:I want to have access to the data outside of the jobtracker function but not output it to a file.
Tom
You need to save the data to a variable that has a scope that makes it available.

$script:data=Receive-Job $job

help scope
User avatar
noescape
Posts: 16
Last visit: Tue Jan 04, 2022 2:17 am

Re: returning of an object from jobtracker completedscript block

Post by noescape »

Thanks for quick help, I already tried that though and it still got thrown out of the window as soon as the completedscript block ended. I will test again tomorrow and let you know.

BTW I didn't add any link to the reply, it's a false anti-spam bug on your forum. I added a powershell code and enclosed it in codebox. I had to remove the whole code to get past that.

Tom
User avatar
noescape
Posts: 16
Last visit: Tue Jan 04, 2022 2:17 am

Re: returning of an object from jobtracker completedscript block

Post by noescape »

The script scope works like you said, so thank you. The problem was that I pre-defined the variable somewhere else without setting the scope and then I was assigning the data to that variable setting the script scope over there instead (I guess it doesn't work that way).
Anyway, thanks for the fast aid, It was getting a little bit frustrating.

Regards,

Tom
This topic is 8 years and 4 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