Delen via


ScriptBlock Class

Definition

An object representing a pre-compiled block of powershell script.

public ref class ScriptBlock : System::Runtime::Serialization::ISerializable
[System.Serializable]
public class ScriptBlock : System.Runtime.Serialization.ISerializable
public class ScriptBlock : System.Runtime.Serialization.ISerializable
public class ScriptBlock
[<System.Serializable>]
type ScriptBlock = class
    interface ISerializable
type ScriptBlock = class
    interface ISerializable
type ScriptBlock = class
Public Class ScriptBlock
Implements ISerializable
Public Class ScriptBlock
Inheritance
ScriptBlock
Attributes
Implements

Remarks

This class track a block of script in a compiled form. It is also used for direct invocation of the script block.

1. Overview

Script block comes in two forms,

a. Full form (cmdlet form)

This comes in following format

{ begin { statementlist; } process { statementlist; } end { statementlist; } }

This form is used for running the script in a pipeline like a cmdlet.

b. Simple form

This comes in following format

{ statementlist; }

2. Script block execution

For the full form (or cmdlet form) of script block, the script block itself is part of a pipeline. Its execution is handled through ScriptCommandProcessor, which involves execution of begin/process/end blocks like a cmdlet. If a scriptblock in simple form is used in a pipeline, its execution is done through ScriptCommandProcessor also, with some of begin/process/end blocks default to be empty.

A script block in simple form can be directly invoked (outside of a pipeline context). For example,

{"text"}.Invoke()

A scriptblock can be directly invoked internally or externally through runspace API.

This class will handle the logic for direct invocation of script blocks.

Constructors

ScriptBlock(SerializationInfo, StreamingContext)
Obsolete.

Protected constructor to support ISerializable.

Properties

Ast

Returns the AST corresponding to the script block.

Attributes

Return all attributes on a script block.

DebuggerHidden

DebuggerHidden.

File

The script file that defined this script block.

Id

The unique ID of this script block.

IsConfiguration

Get/set whether this scriptblock is a Configuration.

IsFilter

Get/set whether this scriptblock is a filter.

Module

Get the PSModuleInfo object for the module that defined this scriptblock.

StartPosition

Return the PSToken object for this function definition...

Methods

CheckRestrictedLanguage(IEnumerable<String>, IEnumerable<String>, Boolean)

Check the script block to see if it uses any language constructs not allowed in restricted language mode.

Create(String)

Create a script block based on a script to be parsed when execution context is provided.

GetNewClosure()

Returns a new scriptblock bound to a module. Any local variables in the callers context will be copied into the module.

GetObjectData(SerializationInfo, StreamingContext)

Support for ISerializable.

GetPowerShell(Boolean, Object[])

Returns PowerShell object representing the pipeline contained in this ScriptBlock, similar to the GetPowerShell() method. If the 'isTrustedInput' flag parameter is set to True, then the GetPowerShell() implementation supports extended conversion operations (such as replacing variable values with their current values) that might otherwise be unsafe if applied to untrusted input.

GetPowerShell(Dictionary<String,Object>, Dictionary<String,Object>, Boolean, Object[])

Returns PowerShell object representing the pipeline contained in this ScriptBlock, using variables supplied in the dictionary.

GetPowerShell(Dictionary<String,Object>, Dictionary<String,Object>, Object[])

Returns PowerShell object representing the pipeline contained in this ScriptBlock, using variables supplied in the dictionary.

GetPowerShell(Dictionary<String,Object>, Object[])

Returns PowerShell object representing the pipeline contained in this ScriptBlock, using variables supplied in the dictionary.

GetPowerShell(Object[])

Returns PowerShell object representing the pipeline contained in this ScriptBlock.

GetSteppablePipeline()

Get a steppable pipeline object.

GetSteppablePipeline(CommandOrigin, Object[])

Get a steppable pipeline object.

GetSteppablePipeline(CommandOrigin)

Get a steppable pipeline object.

Invoke(Object[])

Execute this node with the specified arguments. The arguments show up in the script as $args with $_ being the first argument.

InvokeReturnAsIs(Object[])

Execute this node with the specified arguments. The arguments show up in the script as $args. This overload return the raw (unwrapped) result so it can be more efficient.

InvokeWithContext(Dictionary<String,ScriptBlock>, List<PSVariable>, Object[])

A method that allows a scriptblock to be invoked with additional context in the form of a set of local functions and variables to be defined in the scriptblock's scope. The list of variables may include the special variables $input, $_ and $this.

InvokeWithContext(IDictionary, List<PSVariable>, Object[])

A method that allows a scriptblock to be invoked with additional context in the form of a set of local functions and variables to be defined in the scriptblock's scope. The list of variables may include the special variables $input, $_ and $this.

This overload of the function takes a hashtable and converts it to the required dictionary which makes the API easier to use from within a PowerShell script.

ToString()

Returns the text of the script block. The return value might not match the original text exactly.

Applies to