CmdLets/Master/Invoke-ScriptBlocks.ps1
|
Function Invoke-ScriptBlocks { <# .SYNOPSIS Executes a collection of string-based or script block objects in the current session scope. .DESCRIPTION The Invoke-ScriptBlocks function iterates through an array or collection of script blocks, outputs the text representation of each script block to the host, and executes them sequentially within the current scope using Invoke-Command with the -NoNewScope parameter. .PARAMETER ScriptBlocks A mandatory collection of script blocks or strings representing executable PowerShell code. .EXAMPLE $Blocks = @( "Write-Host 'Starting process...'", "Get-Date" ) Invoke-ScriptBlocks -ScriptBlocks $Blocks Description: Runs each provided script block or command string sequentially in the current scope. .NOTES Prerequisites: - Code executed via string conversion requires proper syntax valid for [ScriptBlock]::Create(). .LINK https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command #> [CmdletBinding()] Param( [Parameter(Mandatory=$true)]$ScriptBlocks ) ForEach ($ScriptBlock in $ScriptBlocks) { Write-Host $ScriptBlock Invoke-Command -ScriptBlock ([ScriptBlock]::Create($ScriptBlock)) -NoNewScope } } |