Extracting a variable from within a scriptblock

Hey all,

I am performing a start-job -scriptblock{} and need to extract the variable that i create inside the script block. I was so sure that this could be done, but for the life of me, cannot work it out…

So inside the script block i build an array using “if” and “else”. After the job is run, i am attempting a receive-job -keep and trying to see how the information gets kept. But it seems the array i create doesnt stay stored in the job.

Any ideas?

send your array to output and capture it outside

start-job { $jobarray = 1..100; $jobarray }
#simulate work
start-sleep -sec 10
$array = get-job | receive-job

That is awesome!

I am about to test this and i will report back, appreciate the help.

Ok so its not working. I suspect i have been staring at this piece of code for to long and need a fresh pair of eyes.

So basically what i am trying to achieve is building a tenant report that will be merged with a on-premise (AD) report in powerBI. I currently have around 90 000 users in my tenant and when i start trying to pull the report, i eventually start getting I/O operation timeout errors. When i did a little research, it seems the moment you go over 50 000 objects, this error can start occuring. I tested just pulling UPN and Immutable ID, and this worked fine. So i figured that if i can use this info and instead I create a start-job with 1000 users at a time, i could circumvent this problem. So in each job i successfully create a new pssession, but is all that seems to run. My info gathering doesnt seem to kick off.

Anything you guys can suggest?

and what line of your code send your $report array to output ?

anyway, you can instead of

$Report = @() 
                        ForEach-Object{ 
                             $Report += New-Object psobject -Property @{ 

just write

                        ForEach-Object{ 
                             New-Object psobject -Property @{ 

and send all of your objects to output stream