10325a appendix
TRANSCRIPT
-
8/10/2019 10325A Appendix
1/25
Appendix A
Additional
Windows PowerShellFeatures
-
8/10/2019 10325A Appendix
2/25
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 systemevents
Describe and use Windows PowerShells text-manipulation
features, including regular expressions, the replaceoperator, and Select-String cmdlet
Use Windows Forms and WindowsPresentation Framework to create agraphical dialog for use in a script
-
8/10/2019 10325A Appendix
3/25
Lesson 1: Additional Features Overview
List and describe some of the additional WindowsPowerShell features
-
8/10/2019 10325A Appendix
4/25
Additional Features
XML Features. PowerShell has the ability to read andinterpret XML documents, and to present the document
information in the form of an object hierarchy.
Advanced Functions.You were briefly introduced to these inthe main portion of the course. These functions are capable ofbehaving almost exactly like a cmdlet, although they arewritten using PowerShells scripting language.
Events.You were introduced to WMI events in the mainportion of the course. You can also write PowerShellcommands that are executed automatically in response togeneral operating system events.
Text Manipulation.Although PowerShell is object-based, youmay need to work with textual data from log files or othersources. The shell offers many features designed to parse andmanipulate text.
Windows Forms.Because it is based on .NET Framework,PowerShell can access the Windows Forms and WPF portionsof .NET Framework, giving you the ability to create graphical
elements for your shell scripts.
-
8/10/2019 10325A Appendix
5/25
Lesson 2: XML Features
Describe and use Windows PowerShells XML manipulationfeatures
-
8/10/2019 10325A Appendix
6/25
XML Documents
The Extensible Markup Language (XML) is a generic set ofrules 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 buildlanguages
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 XMLdocument, and convert that document to a generic objecthierarchy for shell manipulation
It doesnt understand XML application rules, only XML rules
-
8/10/2019 10325A Appendix
7/25
XML Hierarchy
Loading an XML document into PowerShell uses a formatsimilar to the one below, where content is cast to XML:
Accessing data within the loaded hierarchy uses a
bracketed numerical notation, such as:
Piping hierarchy objects to Get-Member will display child
objects and properties:
[xml]$xml = get-content example.xml
$xml.computers.computer[0].name$xml.computers.computer[0].services.service[0].servicename
$xml.computers | get-member
-
8/10/2019 10325A Appendix
8/25
Demonstration: Working with XML
Learn how to use the XML features of Windows PowerShell
-
8/10/2019 10325A Appendix
9/25
Lesson 3: Advanced Functions
Describe and create advanced functions inWindows PowerShell
-
8/10/2019 10325A Appendix
10/25
Functions Review
Recall that the primary difference between a normalfunction 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 greatmodularization without needing to resort to advancedsoftware development skills or .NET Framework familiarity
-
8/10/2019 10325A Appendix
11/25
Verbosity
Advanced functions can respond to the verbose switchwithout requiring special support
Simply use Write-Verbose within your function to writeverbose output:
If the function is run with the verbose switch and the$VerbosePreference variable is set to Continue, yourverbose output will appear
If the function is run without verbose, your verboseoutput will be suppressed
You do not need to define the verbose parameter; it existsautomatically and is handled by the shell
Write-Verbose 'Beginning database query'
-
8/10/2019 10325A Appendix
12/25
Pipeline Parameter Binding
Recall that the two types of pipeline parameter binding areByValue and ByPropertyName
Advanced functions can support both binding types
Parameters are normally accessed within the functionsPROCESS 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 objectthat was piped into the function
Refer to the Student Guide for two examples of functionsthat accept pipeline parameter binding ByValue andByPropertyName
-
8/10/2019 10325A Appendix
13/25
Supporting ShouldProcess
Functions that modify the system in any way should bewritten to support the ShouldProcess shell capability
This capability engages when the whatif and confirmparameters are used
If your function is defined as supporting ShouldProcess, youdo not need to declare whatif or confirm
Declaring support for ShouldProcess is part of the functiondeclaration:
Refer to the Student Guide for further details on thenecessary syntax for supporting whatif and confirm
Function Win32Restart-Computer {[CmdletBinding(
SupportsShouldProcess=$true,
ConfirmImpact="High")]
-
8/10/2019 10325A Appendix
14/25
Data Validation
Additional parameter features can help the shell validateparameter values before the function runs:
You can find more information and parameter options byreading the help files on advanced functions:
[parameter(Mandatory=$true)][string][ValidateSet("Restart","LogOff","Shutdown","PowerOff")]
$action
Help about_functions_advanced*
-
8/10/2019 10325A Appendix
15/25
Demonstration: Advanced Functions
Learn how to use various features of advanced functions
-
8/10/2019 10325A Appendix
16/25
Lesson 4: Responding to Events
Describe the purpose of Windows operating system events
Create a Windows Forms timer and enable it to sendevents
Write Windows PowerShell commands that respond toevents
-
8/10/2019 10325A Appendix
17/25
Events
An event is something that occurs within the operatingsystem, often in response to some real-world interaction.
For example: Moving your mouse pointer over a control like abutton or checkbox. Here, the OS generates MouseMoveevents.
Clicking a control generates a Clickevent.
Internal timers generate a tickevent.
PowerShell can register for notifications of certain eventsfrom certain objects.
You can then have the shell run commands when the eventfires and the event notification is received.
-
8/10/2019 10325A Appendix
18/25
Creating a Timer
The following Windows Forms timer is configured to tickevery ten seconds
This script creates the timer and enables it, as timers aredisabled by default:
Nothing happens initially, because nothing has registeredto receive those events or do anything with them
# create a Windows Forms timer
$timer = new-object Timers.Timer
# set an interval to 10 seconds$timer.interval = 10000$timer.enabled = $true
-
8/10/2019 10325A Appendix
19/25
Demonstration: Working with Events
Learn how to use events within the shell
-
8/10/2019 10325A Appendix
20/25
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
-
8/10/2019 10325A Appendix
21/25
Text Manipulation
The shell includes numerous text manipulation features,including:
Within the shell, strings are objects. Try piping Helloto Get-Member to see the various properties and methods that allowyou to manipulate strings.
The replace, join, and split operators provide easy ways toperform commonly-needed text manipulation functions.
Regular expressions use industry-standard regex syntax toperform pattern matching on strings.
The Select-String cmdlet can perform both simple and regularexpression matches against text, including text that exists infiles on disk.
-
8/10/2019 10325A Appendix
22/25
Demonstration: Manipulating Text
Learn how to use various text manipulation techniqueswithin the shell
-
8/10/2019 10325A Appendix
23/25
Lesson 6: Using Windows Forms
Based on an example script, write a script that utilizesgraphical user interface elements to collect and display
user data
-
8/10/2019 10325A Appendix
24/25
Windows Forms and WPF
Microsoft .NET Framework contains a subset of elementscalled Windows Forms
These are used to construct GUI elements such as dialogboxes
Newer versions of the .NET Framework include theWindows Presentation Framework (WPF)
WPF accomplishes the same thing as Windows Forms, but in adifferent 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
-
8/10/2019 10325A Appendix
25/25
Demonstration: Windows Forms
Learn how to use GUI elements within a script