Some PowerShell CmdLets have as parameter ScriptBlock and as such expects ScriptBlock data type for this parameter. Let me illustrate that with one example.
Invoke-Command CmdLet has ScriptBlock parameter that expects ScriptBlock data type:
Invoke-Command -ScriptBlock { Get-Process }
Here is the resultset:
Sometimes we have very long and complex script blocks and for that reason, it is smart to have own CmdLet that will convert string to Script block.
Table of Contents
Convert-StringToScriptBlock CmdLet – Explained
We have written our own PowerShell CmdLet Convert-StringToScriptBlock that converts a string into script block data type. It is very simple and short but very efficient so let me explain the code briefly.
Convert-StringToScriptBlock CmdLet belongs to Efficiency Booster PowerShell Project. This project is the library of different CmdLets that can help us IT personal to do our everyday tasks more efficiently and accurately.
Source code for Convert-StringToScriptBlock CmdLet can be downloaded from this zip file so please feel free to download it and it would be easier for you to follow me along.
Convert-StringToScriptBlock CmdLet is part of Common module and if you have downloaded the source code it can be found in the folder …\[My] Documents\WindowsPowerShell\Modules\03common
INFO: If you want to know how to install and configure Efficiency Booster PowerShell Project files please read the following article: How To Install And Configure PowerShell: CmdLets, Modules, Profiles.
Convert-StringToScriptBlock – Input Parameters
As input parameter we have only one and that is:
- string – parameter accepts string value using PowerShell Pipeline and it is not mandatory
Here is the parameters definition code:
param (
[Parameter(ValueFromPipeline=$true,
Position=0,
Mandatory=$false,
HelpMessage="Input string that will this CmdLet convert into Script Block.")]
[string]$string
)
INFO: PowerShell Pipelining is a very important concept and I highly recommend you to read the article written on the subject. How PowerShell Pipeline Works. Here I have shown in many examples the real power of PowerShell using the Pipelining.
Convert-StringToScriptBlock – PROCESS Block
In the PROCESS block is the all functionality of this CmdLet. We simply use the Create method of ScriptBlock class to convert a string into the ScriptBlock data type.
Here is the source code of PROCESS block:
PROCESS {
Write-Verbose "Starting converting string to script block..."
$sb = [scriptblock]::Create($string)
Write-Verbose "Converting string to script block finished."
return $sb
}
Convert-StringToScriptBlock – BEGIN And END Blocks
BEGIN and END blocks are empty.
INFO: To understand BEGIN, PROCESS and END blocks in PowerShell please read PowerShell Function Begin Process End Blocks Explained With Examples.
Comment-Based Help Section
For every one of my own CmdLets, I write Comment-Based help as well.
INFO: If you want to learn how to write comment-based Help for your own PowerShell Functions and Scripts please read these articles How To Write PowerShell Help (Step by Step). In this article How To Write PowerShell Function’s Or CmdLet’s Help (Fast), I explain the PowerShell Add-on that help us to be fast with writing help content.
How To Use Convert-StringToScriptBlock CmdLet – Tips
Here is a very simple example that shows the use of Convert-StringToScriptBlock CmdLet. Basically, we list all the Windows Services on the local machine using Invoke-Command CmdLet.
$sb = "Get-Service" | Convert-StringToScriptBlock
Invoke-Command -ScriptBlock $sb
Here is the resultset of above call:
Useful PowerShell Convert String To ScriptBlock Articles
Here are some useful articles and resources:
Convert-StringToScriptBlock CmdLet Source Code
DISCLAIMER: Convert-StringToScriptBlock function is part of the Efficiency Booster PowerShell Project and as such utilize other CmdLets that are part of the same project. So the best option for you in order for this function to work without any additional customization is to download the source code of the whole project from here.
INFO: My best advice to every PowerShell scripter is to learn writing own PowerShell Advanced Functions and CmdLets and I have written several articles explaining this, so please read them. How To Create A Custom PowerShell CmdLet (Step By Step). Here I explain how to use PowerShell Add-on Function to be faster in writing PowerShell Functions How To Write Advanced Functions Or CmdLets With PowerShell (Fast).
Here is the source code of the whole Convert-StringToScriptBlock CmdLet:
<#
.SYNOPSIS
Convert string value passed by pipeline into script block value.
.DESCRIPTION
Convert string value passed by pipeline into script block value.
I have used this CmdLet in Get-FileProperties from 03 Common module.
It can be also used for calls to Invoke-Command cmdlet which accepts script block value in the parameter ScriptBlock.
For example, the Invoke-Command cmdlet has a ScriptBlock parameter that takes a script block value, as shown in this example:
Invoke-Command -ScriptBlock { Get-Process }
.PARAMETER string
Input value of string that will be converted into script block type value.
.EXAMPLE
$sb = "Get-Service" | Convert-StringToScriptBlock
Invoke-Command -ScriptBlock $sb
Description:
------------------------------------------------------
Convert string value "Get-Service" into script block and keep in $sb variable.
Run Invoke-Command cmdlet and use value in $sb variable as input for parameter ScriptBlock.
.EXAMPLE
Help Convert-StringToScriptBlock -Full
Description:
------------------------------------------------------
Test of Powershell help.
.INPUTS
System.String
.OUTPUTS
ScriptBlock
.NOTES
FunctionName : Convert-StringToScriptBlock
Created by : Dejan Mladenovic
Date Coded : 10/31/2018 19:06:41
More info : http://improvescripting.com/
.LINK
How To Convert String To Script Block Using PowerShell
Invoke-Command
#>
Function Convert-StringToScriptBlock {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline=$true,
Position=0,
Mandatory=$false,
HelpMessage="Input string that will this CmdLet convert into Script Block.")]
[string]$string
)
BEGIN {
}
PROCESS {
Write-Verbose "Starting converting string to script block..."
$sb = [scriptblock]::Create($string)
Write-Verbose "Converting string to script block finished."
return $sb
}
END {
}
}
#region Execution examples
#Convert-StringToScriptBlock -string "Parameter value" -Verbose
#endregion