Search notes:

System.Management.Automation.ScriptBlock (class)

A System.Management.Automation.ScriptBlock is a pre-compiled PowerShell script block:
PS C:\> $scriptBlock = { write-host 'Hello world' }
PS C:\> $scriptBlock.GetType().FullName
System.Management.Automation.ScriptBlock
PS C:\> 1 .. 3 | forEach-object $scriptBlock
Hello world
Hello world
Hello world

Properties and Methods

Ast
Attributes
CheckRestrictedLanguage()
Create()
DebuggerHidden
File
GetNewClosure()
GetPowerShell()
GetSteppablePipeline()
Id
Invoke()
InvokeReturnAsIs()
InvokeWithContext()
IsConfiguration
IsFilter
Module
StartPosition
ToString()

GetNewClosure

$var = 'initial value for variable var'
$sb = {
   write-host "Script block is invoked"
   write-host "  var = $var"
   if (test-path variable:xyz) {
       write-host "  The variable xyz is defined, it's value is $xyz"
   }
   else {
       write-host "  The variable xyz is not defined"
   }
}

$sb_closure = $sb.GetNewClosure()


$var = 'changed value for variable var'
$xyz = 'Value for xyz'

& $sb
& $sb_closure
If this code is placed into a script and executed, it prints:
Script block is invoked
  var = changed value for variable var
  The variable xyz is defined, it's value is Value for xyz
Script block is invoked
  var = initial value for variable var
  The variable xyz is not defined

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Microsof...', 1745434329, '3.140.192.22', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Microsoft/dot-net/namespaces-classes/System/Management/Automation/ScriptBlock(95): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78