Quick and Dirty Scripting

A blog that focuses on automating system administration tasks for Linux, Windows, and VMware ESX

Sunday, February 10, 2008

 

Powershell Scriptblocks

One of the interesting features that is, I believe, little know in Powershell is the scriptblock. What is a scriptblock? The way that I define it is a function or block of code that is assigned to a variable. Why do you need to know about this? I've found it most useful when using Powershell to create a GUI. You have to assign an action to an element on the GUI (like a Button). But the action can not be a function. It must be a script block. You can define the block as a variable or define it within the {} of the Add_Click method of the System.Windows.Forms.Button object.

Now I'm probably completely wrong with this, and if I am I would love to learn more about it. So please let me know! I want to cover how to create GUIs in Powershell in couple weeks so I will be building off this blog post.

So how do you create a Script block. Its easy as assigning a variable to a anything within {}.

A simple case would be something like
PS C:\temp> $x = { Write-Host "Hello, World!" }

PS C:\temp> $x.GetType()

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False ScriptBlock System.Object



If you just call the variable, it will return the block definition

PS C:\temp> $x
Write-Host "Hello, World!"

To invoke the scriptblock, you need to call the Inovke() method of the script

PS C:\temp> $x.Invoke()
Hello, World!


Within the scriptblock, you can do whatever you want to do. It can be multiple lines, you can even call functions from within the block. For example:

PS C:\temp> function y () {
>> Write-Host "Within function y"
>> }
>>
PS C:\temp> y
Within function y
PS C:\temp> $x = {
>> Write-Host "Within scriptblock x"
>> y
>> }
>>
PS C:\temp> $x.Invoke()
Within scriptblock x
Within function y

Here are all of the methods that Scriptblocks provides within Powershell

PS C:\temp> $x | Get-Member


TypeName: System.Management.Automation.ScriptBlock

Name MemberType Definition
---- ---------- ----------
Equals Method System.Boolean Equals(Object obj)
GetHashCode Method System.Int32 GetHashCode()
GetType Method System.Type GetType()
get_IsFilter Method System.Boolean get_IsFilter()
Invoke Method System.Collections.ObjectModel.Collection`1[[System.Management.Automation.PSObject, Syst...
InvokeReturnAsIs Method System.Object InvokeReturnAsIs(Params Object[] args)
set_IsFilter Method System.Void set_IsFilter(Boolean value)
ToString Method System.String ToString()
IsFilter Property System.Boolean IsFilter {get;set;}


I hope this gives you an introduction into scriptblocks. Next time, hopefully, we can cover the TCL/TK-like feature of Powershell - its GUI.





Comments: Post a Comment





<< Home

Archives

October 2007   November 2007   December 2007   January 2008   February 2008   April 2008   October 2009  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]