2

I am running a PowerShell script with around 10,000 lines of code, and I noticed that Event ID 4104 is being generated during the execution. In each Event ID 4104 entry, I see the following message:

"Creating Scriptblock text (12 of 12): ..."

I have a few questions regarding this:

What is a Scriptblock in PowerShell? I understand that PowerShell executes code within Scriptblocks, but I'm not entirely sure about the detailed working and purpose of a Scriptblock. Can someone clarify this?

What is the basis for the Scriptblock being divided into "12"? I’m seeing "Creating Scriptblock text (12 of 12)" in the event. How are Scriptblocks divided into multiple parts, and why is it showing "12" here? Does it refer to the number of segments in the script or something else?

What is the maximum size of a Scriptblock in PowerShell? Is there any limit to the size of a Scriptblock, and if so, what’s the maximum allowed size? Is this related to the division into parts?

3
  • 2
    The basis for splitting is the length of the payload (eg. the script source code) - each ETW Trace event has a maximum size (~32KB), so PowerShell's event tracer splits the payload into chunks smaller than 32KB, and emits an event for each of "X of Y parts" of the scriptblock text. The same way a text editor might wrap a line beyond a certain length Commented Feb 18 at 9:26
  • 1
    Re what a script block is: All units of executable PowerShell code (beyond a single command or expression) are ultimately [scriptblock] instances, be they script-block literals ({ ... }), functions, or script files, the latter two types being named script blocks. To obtain the [scriptblock] instance underlying a function or script file, use, e.g. (Get-Command oss).ScriptBlock or (Get-Command $PROFILE).ScriptBlock Commented Feb 18 at 15:16
  • 2
    As Mathias' comment implies, the size limit is imposed by the ETW (Event Tracing for Windows) system component, on logging the source code of a script block being executed, not by PowerShell. I'm not aware of any size limit imposed by PowerShell itself; the limit may simply be the amount of available memory, meaning that, pragmatically speaking, there's virtually no limit. Commented Feb 18 at 15:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.