Trouble with Nested ScriptBlocks in PowerShell Jobs

0
4
Asked By CuriousCoder42 On

Hey everyone! I'm trying to figure out how nested scriptblocks work within the Start-Job cmdlet in PowerShell. I've got a parent scriptblock called JobScript that starts a job, and within that, I defined two nested scriptblocks: NestedScript and NestedScript2. The issue arises when NestedScript2 attempts to call NestedScript using Invoke-Command; it always returns blank. I tried using the "$using:" prefix, but it doesn't seem to apply here since both are defined in the same context. I've also experimented with passing NestedScript as a parameter through NestedScript2, but I keep hitting a serialization issue when I try to do this. My main goals are: 1) to understand why $NestedScript ends up blank when referenced from $NestedScript2 and 2) whether there's a better way to approach this. Looking forward to any insights you might have! Thanks!

4 Answers

Answered By PowerShellNinja On

This does seem a bit convoluted. I suggest defining $NestedScript inside $NestedScript2 or adjusting its scope with $script:: or $global::. This might help with resolving the variable across the scriptblocks better.

CuriousCoder42 -

Appreciate the suggestion! I tried using $script: and $global: but didn't see much improvement. Also, moving $NestedScript inside $NestedScript2 led to more complications. I’ll keep experimenting though.

Answered By DevJim On

My advice is to avoid nesting when possible. Jobs are designed to run concurrently without too many dependencies. You can get job progress by querying the job’s state instead. For monitoring, consider running a separate job that collects outputs instead of trying to nest your scripts. That way, you won't run into serialization problems as much.

CuriousCoder42 -

Thanks for the tips! I'm trying to execute multiple PowerShell DSC jobs simultaneously to save time. Since I'm triggering these from Jenkins, I want to gather all outputs there for monitoring. I guess limiting nesting could simplify things!

JobWatcher98 -

That's a solid approach! Try to keep your workflows as simple as you can to make it easier to manage outputs.

Answered By TechieTed On

Here are a few points to consider: 1. Use the -AsJob parameter with Invoke-Command; it simplifies job definitions. 2. Be cautious of nested remote calls; they can lead to double-hop issues. 3. ActiveDirectory can't be installed with Install-Module remotely. It's better to add necessary modules locally. 4. If you need to run AD commands remotely, use the -PSSession parameter to specify a session for easier management.

CuriousCoder42 -

Good call on the -AsJob flag, I’d overthought that. I know about the double-hop issue; it's definitely something I'm battling with due to the nesting. And I appreciate the heads-up about the ActiveDirectory module.

Answered By ScriptyMcScriptface On

I tested a simplified version of your code on PowerShell 5.1, and everything seems to work fine for me. Here's what I did: I defined both NestedScript and NestedScript2 within JobScript, and when I invoked them, they returned the expected outputs. So, it looks like whatever's causing the issue might not be related to the structure of your scriptblocks. Maybe there's a different thing in your context?

CuriousCoder42 -

Thanks for checking that out! Your version runs perfectly on my end too. I’ll dig deeper into my setup to see what might be causing the issue.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.