This PowerShell module creates and runs scripts from the input pipeline.
New-ScriptBlock [-InputString] <string>
Invoke-ScriptBlock -InputString <string> [-ArgumentList <Object[]>] [-NoNewScope]
Invoke-ScriptBlock -ScriptBlock <scriptblock> [-ArgumentList <Object[]>] [-NoNewScope]
The instantiation cmdlet uses ScriptBlock.Create to create the script.
The invocation cmdlet uses Invoke-Command to run the script.
Pipe a script and invoke with given arguments.
@'
param($Message)
Write-Output -InputObject $Message
'@ | Invoke-ScriptBlock -ArgumentList 'Hello World'
Build in the ScriptBlock
subdirectory with
$ dotnet publish --configuration Release
Install by copying the module into a directory on the PSModulePath
This uses rhubarb-geek-nz.Console to provide POSIX like stdin
, stdout
and stderr
access.
- Script is read from
stdin
usingRead-Console
- It is invoked by
Invoke-ScriptBlock
. - Arguments are passed via
$MyInvocation
- Output is written through
Write-Console
to split tostdout
andstderr
. - try/catch is used to set
LastExitCode
onWrite-Error
#!/usr/bin/env pwsh
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
& {
try
{
Read-Console | Invoke-ScriptBlock -ArgumentList $global:MyInvocation.UnboundArguments
Set-Variable -Name LastExitCode -Scope Global -Value 0
}
catch
{
Set-Variable -Name LastExitCode -Scope Global -Value 1
$PSItem
}
} *>&1 | Write-Console
Exit $LastExitCode