ConvertTo-ScriptBlock

Ok, just a small one today. You won’t save any typing with this, as the actual code is just one line, and not much longer than the function name. But I think it looks nicer, and it’s easier to remember. Besides, it looks more like “proper” PowerShell.

This function takes input and converts it to a ScriptBlock. Nothing more, nothing less.

function ConvertTo-ScriptBlock{
<#
.SYNOPSIS
Convert to ScriptBlock.
.DESCRIPTION
Convert input to ScriptBlock.
.EXAMPLE
Get-Content '.\scriptFile.ps1' -raw | ConvertTo-ScriptBlock
Converts a script file to a ScriptBlock.
.NOTES
Author: Øyvind Kallstad
Date: 13.03.2014
Version: 1.0
#>
param (
# Input you want converted to a ScriptBlock.
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]$InputObject
)
try {
$scriptBlock = [ScriptBlock]::Create($inputObject)
}
catch {
Write-Warning $_.Exception.Message
}
Write-Output $scriptBlock
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s