« Previous 1 2 3 Next »
Innovations in PowerShell 5
Upshift
Reusable Selection Lists
Another innovation relates to the task of creating reusable selection lists as a list of constants. The name of this constructor is Enum
. The separator character is the line break, which makes the enumerator list similar to an array. However, the element type cannot be defined – it is always an integer (Listing 2).
Listing 2
Using Enums
01 Enum MemberServer { 02 SRV01 03 SRV02 04 SRV03 05 SRV04 06 } 07 08 $Remote = [MemberServer]::SRV02; 09 [enum]::IsDefined(([MemberServer]),$Remote) #Type, Member
The reason these new constructs exist is to allow programmers to create their own DSC resources, types, and exception handlers – although the focus is on DSC. If you take a look at the help, using get-help about_Classes
, this becomes more than clear. Thus far, a programmer's own configuration type – and a DSC resource is precisely that – relied on the interaction between PowerShell and "Managed Object Files" (MOF), an unwieldy Windows Management Instrumentation (WMI) construct. The major difference between using a cmdlet-based DSC resource and a class is that you do not need an MOF file.
Instead, the control information resides in the defined interfaces, thus making the information container unnecessary. Subordinate DSC resources within the PowerShell are no longer needed. At the same time, multiple DSC resources can be stored within a single module. In this respect, PowerShell 5 is a consistent advancement of the predecessor versions and makes handling a key feature like DSC easier and more elegant.
PowerShell as a Text Processor
PowerShell 2 implemented the processing of structured data through a cmdlet, and entering XML and CSV data has been unproblematic ever since – as has the task of converting console output to these formats. In many cases, however, the return value from the commands is non-standard text. Downstream processing then requires fairly complex programming routines. This gap has been plugged by the new ConvertFrom-String
cmdlet. The objective is to convert arbitrary text content into objects. The technology is based on FlashExtract
, a string-parsing tool.
The new PowerShell command has parameters for managing string analysis. The ability to use templates is particularly impressive. For example, a one-off return value can be used to generate a template, which is then used repeatedly for the data type in question. Administrators will appreciate this new cmdlet if they need to analyze the return value of a command. If the command returns objects, these are easier to process downstream. The example:
Get-ChildItem -Path C:\Windows-Filter *.xml -recurse | sort-Object -Property length | Select-Object -First 2
makes the benefits of an object-based approach clear. All it does is output the two largest XML files below C:\Windows
. But what happens to the return value from a command such as netstat -a
or ping
?
These commands output a sequence of strings that needs to be evaluated using regular expressions. ConvertFrom-String
converts this collection of strings into a new type. You can define the properties yourself, or use a template to determine them; for example,
$MyObj = "192.168.1.0 srv DNS" | convertFrom-String -PropertyNames IP, Name, Service $MyObj.IP
converts simple, space-separated text into an object. Access to the individual columns is via the designators stated in the PropertyNames
parameter. Downstream processing is now simple and familiar. Templates are useful for more complex tasks because they can assign attribute names to the expected characters.
Archiving and Links
The developers of Windows PowerShell have finally gotten around to tackling the task of handling archives in the current version. So far, administrators wanting to compress files often resorted to the PowerShell Community Extensions module, which has had the ability to create ZIP and RAR files for some time now.
The Microsoft.PowerShell.Archive
module is somewhat more spartan in its feature set. The only supported format right now is ZIP, but the syntax is quite intuitive. The two new cmdlets, Archive
and Expand-Archive
only need parameter values for the source and target of the operation:
Compress-Archive -Path C:\template.txt -DestinationPath C:\udat\t.zip -update
This instruction lets you create a ZIP archive from the template.txt
file.
When creating elements, especially on the filesystem, the selection of the element type was formerly restricted to physical objects. For example, you could only choose between the file
and directory
types. The New-Item
cmdlet offers a slightly more convenient approach. A new value that is now supported for the ItemType
parameter is SymbolicLink
; the name is fairly self-explanatory.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy ADMIN Magazine
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Most Popular
Support Our Work
ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.