PowerShell CTP3

There was an early Christmas present from the Windows PowerShell (AKA PoSH) Team.  The Community Technology Preview 3 (CTP3) of Windows PowerShell v2.0 was released on December 23rd just in time for Christmas.  The announcement is here.  As expected CTP3 builds on the new technology provided in CTP2 which was released in May 2008.  You can download CTP3 from the Microsoft Download Center.

Hemant Mahawar, Program Manager for PowerShell, summarized the CTP3 release as follows:

This release brings, among other things, performance improvements ... things will be faster/more efficient than before. PowerShell remoting now allows implicit remoting where command execution appears to be local even though they are remote. We have added over 60 new cmdlets in this release ... cmdlets for adding/removing/renaming computers, cmdlets for event logs, cmdlets for WS-Man functionality and even a WS-Man provider. The “graphical” host, Windows PowerShell ISE, now supports a graphical debugger, context sensitive F1 help and a programmable interface for you to party on.
I tested CTP3 on Vista Ultimate SP1.  The only issue I encountered when installing CTP3 was that fact that CTP3 did not honor the execution policy set by me in CTP2 contary to what was stated in the Release Notes.  Moreover, it was impossible to set the execution policy to unrestricted using Set-Executionpolicy.



After digging around in the registry hives, the problem became apparant.  The PowerShell execution policy is set correctly in [HKEY_LOCAL_MACHINE\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell\executionPolicy] but not in [HKEY_CURRENT_USER\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell\executionPolicy].  I found this registry entry had to manually changed from allsigned to unrestricted.

One major enhancement in this release relates to remoting and background jobs.  Both require that you install Windows Remote Management (WinRM) 2.0 CTP3.  Currently WinRM 2.0 CTP3 is supported only on Windows Vista SP1 and on Windows Server 2008.  For some reason that I do not yet understand background jobs, even the jobs only run on the local computer, rely on the remoting features of PowerShell.

The othere major enhancement relates to what was known as Script CmdLets in CTP2.  They have been renamed to advanced functions in CTP3.  Advanced functions are functions that have the same capabilities and behaviors as cmdlets but are written using the PowerShell scripting language instead of a compiled language such as C#.

There are two types of advanced functions, i.e. named functions and unnamed functions.   Both types use the CmdletBinding attribute to identify themselves as advanced functions that act similar to compiled cmdlets.  Both types can also be used within a script file.  The difference is that running a script file only declares a named function but an unnamed function is actually invoked.  The CmdletBinding attribute is similar to the Cmdlet attribute used in compiled cmdlet classes to identify the class as a cmdlet.  For further information on Advanced Functions, type man about_functions_advanced.

Here is a simple CTP3 script which defines a single advanced function named get-myrandom that returns a pseudo-random number.
#
# Sample PowerShell v2.0 CTP3 script
#

Set-StrictMode -Version Latest

DATA msgText {
#culture="en-US"
ConvertFrom-StringData -stringdata @'
msg001 = Thank you for trying this script.
'@
}

function get-myrandom {

<#
.Synopsis
Generate a random number
.Description
This is a simple function to demonstrate some of the
PowerShell v2.0 advanced functions capabilities by
generating a random number.
.Parameter Max
Maximum value of the generated random number
.Example
PS> get-myrandom 46
.InputType
[int]
.ReturnValue
[int]
.Link
about_functions
about_functions_advanced
about_functions_advanced_methods
about_functions_advanced_parameters
about_script_internationalization
.Notes
AUTHOR: Finnbarr P. Murphy
DATE: 12/27/2008
REQUIRES: v2.0 CTP3
#>

[CmdletBinding(SupportsShouldProcess=$true)]
PARAM(
[Parameter(Mandatory=$true)]
[ValidateRange(50,99)]
[int]
$max
)

Begin {
import-localizeddata -bindingVariable msgText
$random = new-object System.Random
}

Process {
$random.Next($max)
}

End {
$msgText.msg001
}
}
The DATA section contains text strings which are replaced with the appropriate translated strings if available depending on the locale (cultural) settings of your computer.  ConvertFrom-StringData converts the text strings into dictionary-like hash tables to facilitate translation.  The code import-localizeddata -bindingVariable msgText does the actual work of retrieving the translated text strings from the appropriate myrandom.psd1 file in the locale specific subdirectories as shown below.
C:\Scripts
C:\Scripts\myrandom.ps1
C:\Scripts\de-DE\myrandom.psd1
C:\Scripts\ar-SA\myrandom.psd1
C:\Scripts\zh-CN\myrandom.psd1
The text between the comment start and end tags is parsed and displayed as help text when get-myrandom is queried using man or get-helpThe pseudo random number generator is instantiated as an object in the Begin{} block.  The actual work of the function, as far as the user is concerned, is performed in the Process{} block which returns a generated pseudo-random number.  Finally the Process{} outputs a locale appropriate text string if available otherwise the default text string provided in the DATA section.

One small nit.  I noticed that the TechNet online documentation for PowerShell has not been updated to reflect CTP3.  Also much of the built-in help documentation supplied with CTP3 is quite frankly incorrect.   Hopefully the PowerShell team will clean up the documentation before long.

BTW, for those of you who do not have PowerShell installed on your computer, Microsoft TechNet has a PowerShell Scripting Online Virtual Lab which is available to anybody who wants to learn about and try out PowerShell scripting.  Currently it uses Powershell v1.0.

As always, enjoy!

0 comments:

Post a Comment