Вы находитесь на странице: 1из 25

Appendix A

Additional
Windows PowerShell
Features

Appendix Overview
Explain the use of Windows PowerShells XML features
Create advanced functions that behave like cmdlets

Write functions or scripts that respond to operating system

events

Describe and use Windows PowerShells text-manipulation

features, including regular expressions, the replace


operator, and Select-String cmdlet

Use Windows Forms and Windows

Presentation Framework to create a


graphical dialog for use in a script

Lesson 1: Additional Features Overview


List and describe some of the additional Windows

PowerShell features

Additional Features

XML Features. PowerShell has the ability to read and


interpret XML documents, and to present the document
information in the form of an object hierarchy.

Advanced Functions. You were briefly introduced to these in


the main portion of the course. These functions are capable of
behaving almost exactly like a cmdlet, although they are
written using PowerShells scripting language.

Events. You were introduced to WMI events in the main


portion of the course. You can also write PowerShell
commands that are executed automatically in response to
general operating system events.

Text Manipulation. Although PowerShell is object-based, you


may need to work with textual data from log files or other
sources. The shell offers many features designed to parse and
manipulate text.

Windows Forms. Because it is based on .NET Framework,


PowerShell can access the Windows Forms and WPF portions
of .NET Framework, giving you the ability to create graphical
elements for your shell scripts.

Lesson 2: XML Features


Describe and use Windows PowerShells XML manipulation

features

XML Documents
The Extensible Markup Language (XML) is a generic set of

rules that describe how to build text-based documents


that can contain hierarchical data

XML is not any one format

It is a grammar, a set of rules that describe how to build


languages

A use of XML is called an XML application

XHTML is an XML-based language used by Web browsers

Other XML applications exist for many other uses

PowerShell can read almost any well-formed XML

document, and convert that document to a generic object


hierarchy for shell manipulation

It doesnt understand XML application rules, only XML rules

XML Hierarchy
Loading an XML document into PowerShell uses a format

similar to the one below, where content is cast to XML:


[xml]$xml = get-content example.xml

Accessing data within the loaded hierarchy uses a

bracketed numerical notation, such as:

$xml.computers.computer[0].name
$xml.computers.computer[0].services.service[0].servicename
Piping hierarchy objects to Get-Member will display child

objects and properties:

$xml.computers | get-member

Demonstration: Working with XML


Learn how to use the XML features of Windows PowerShell

Lesson 3: Advanced Functions


Describe and create advanced functions in

Windows PowerShell

Functions Review
Recall that the primary difference between a normal

function and an advanced function are the cmdlet-style


parameter definitions in advanced functions

This allows for named parameters, positional parameters,


pipeline parameter binding, and simple data validation

Advanced functions can support

confirm

whatif

verbose

ErrorAction

PowerShell advanced functions provide great

modularization without needing to resort to advanced


software development skills or .NET Framework familiarity

Verbosity
Advanced functions can respond to the verbose switch

without requiring special support

Simply use Write-Verbose within your function to write


verbose output:
Write-Verbose 'Beginning database query'

If the function is run with the verbose switch and the

$VerbosePreference variable is set to Continue, your


verbose output will appear

If the function is run without verbose, your verbose

output will be suppressed

You do not need to define the verbose parameter; it exists


automatically and is handled by the shell

Pipeline Parameter Binding


Recall that the two types of pipeline parameter binding are

ByValue and ByPropertyName

Advanced functions can support both binding types

Parameters are normally accessed within the functions


PROCESS script block

Within that script block, your pipeline input parameter will


contain only one value at a time

The PROCESS script block will execute once for each object
that was piped into the function

Refer to the Student Guide for two examples of functions

that accept pipeline parameter binding ByValue and


ByPropertyName

Supporting ShouldProcess
Functions that modify the system in any way should be

written to support the ShouldProcess shell capability

This capability engages when the whatif and confirm


parameters are used

If your function is defined as supporting ShouldProcess, you


do not need to declare whatif or confirm

Declaring support for ShouldProcess is part of the function

declaration:

Function Win32Restart-Computer {
[CmdletBinding(
SupportsShouldProcess=$true,
ConfirmImpact="High"
)]
Refer to the Student Guide for further details on the

necessary syntax for supporting whatif and confirm

Data Validation
Additional parameter features can help the shell validate

parameter values before the function runs:

[parameter(Mandatory=$true)]
[string]
[ValidateSet("Restart","LogOff","Shutdown","PowerOff")]
$action

You can find more information and parameter options by

reading the help files on advanced functions:

Help about_functions_advanced*

Demonstration: Advanced Functions


Learn how to use various features of advanced functions

Lesson 4: Responding to Events


Describe the purpose of Windows operating system events
Create a Windows Forms timer and enable it to send

events

Write Windows PowerShell commands that respond to

events

Events
An event is something that occurs within the operating

system, often in response to some real-world interaction.

For example: Moving your mouse pointer over a control like a


button or checkbox. Here, the OS generates MouseMove
events.

Clicking a control generates a Click event.

Internal timers generate a tick event.

PowerShell can register for notifications of certain events

from certain objects.

You can then have the shell run commands when the event
fires and the event notification is received.

Creating a Timer
The following Windows Forms timer is configured to tick

every ten seconds

This script creates the timer and enables it, as timers are
disabled by default:
# create a Windows Forms timer
$timer = new-object Timers.Timer
# set an interval to 10 seconds
$timer.interval = 10000
$timer.enabled = $true

Nothing happens initially, because nothing has registered

to receive those events or do anything with them

Demonstration: Working with Events


Learn how to use events within the shell

Lesson 5: Text Manipulation Features


Review methods of the String object
Use the replace, split, and join operators

Use regular expression matching


Use the Select-String cmdlet

Text Manipulation
The shell includes numerous text manipulation features,

including:

Within the shell, strings are objects. Try piping Hello to GetMember to see the various properties and methods that allow
you to manipulate strings.

The replace, join, and split operators provide easy ways to


perform commonly-needed text manipulation functions.

Regular expressions use industry-standard regex syntax to


perform pattern matching on strings.

The Select-String cmdlet can perform both simple and regular


expression matches against text, including text that exists in
files on disk.

Demonstration: Manipulating Text


Learn how to use various text manipulation techniques

within the shell

Lesson 6: Using Windows Forms


Based on an example script, write a script that utilizes

graphical user interface elements to collect and display


user data

Windows Forms and WPF


Microsoft .NET Framework contains a subset of elements

called Windows Forms

These are used to construct GUI elements such as dialog


boxes

Newer versions of the .NET Framework include the

Windows Presentation Framework (WPF)

WPF accomplishes the same thing as Windows Forms, but in a


different manner

A complete discussion on Windows Forms or WPF is

outside the scope of this course

However, using examples in the following demo, you can see


how Windows Forms work and can discuss WPF

Demonstration: Windows Forms


Learn how to use GUI elements within a script