详细说明:This document assumes you are working on a University of Edinburgh Sup-
ported Windows Desktop. The exercises may work in other environments
but this has not been tested. If following this document you may need to
search the Internet for additional modules and follow the instructions on how
to insta11. 4 Exercis
11.5 Advanced: Handling Xml data and loading a. Net framework 28
12 Further He
30
13 Listings
32
1 About Power shell
Power Shell is a registered trademark of Microsoft Corporation. Power Shell
s provided by Microsoft Corporation and is a well thought out, very usable
and powerful scripting language. The influence of other scripting languages
not just Microsoft can be seen
In Power Shell commands(often called cmdlets) have the form
verb-noun
that is a verb indicating the action and a noun indicating the object for
example
get-childitem
Power Shell itself is provided as a command line environment. In addi
tion to all the Power"" one may call a non-Power Shell program
directly (e.g. notepad. exe)
2 Getting started
This document assumes you are working on a University of Edinburgh Sup-
ported Windows Desktop. The exercises may work in other environments
but this has not been tested. If following this document you may need to
search the internet for additional modules and follow the instructions on how
to install them. Unless you know what you are doing, it is sensible to only
install extra Power Shell functionality from genuine Microsoft sites
Readers will find the exercises easier if they have previous experience of
scripting or programming languages. Additionally, Power Shell uses pipelines
extensively so prior attendance at Unix 1 and Unix 2 courses will be and
advantage
1. Click on the start but
2. Type Power shell
3. You should now see
Windows Power Shell(figure 1)
Windows Power Shell ISE(Figure 2)
2y Wir dows Pcwershel
Figure 1: Power Shell -the command line environment
If running scripts, the first option of using the Power Shell directly is fine
For creating and editing scripts the Power Shell ISE(Integrated Scripting
Environment )is very useful
You may see additional options but these are the two to use for this
course
2.1 Exercise
1. Run Power Shell. Type exit and press the Enter key. What happens?
2. Run PowerShell ISE. Click on the Help menu. Click on Update Win-
dows Power Shell Help. You will notice in the window below a com.
mand update-help runs and then usually produces an error. This is
normal on the University of Edinburgh Supported windows Desktop
3. Read the red error text. Can you determine why the comnand failed?
a Windows Powershell ISE
file Edit Vicw Tools Debug Add ons +clp
日40入0p日目口世
Run script (F5)
Untitled.ps1 x
Sday= tuesday
2 Sday
PS M: \> Sday="tuesday
Sday
Completed
I Ln 2 Cal 5 I
170%
Figure 2: Power Shell TSE- An integrated script, editor
3 commands
Commands may take none, one or several parameters and one, none or several
values(see Listing??, page??). Users of Unix shell environments will quickly
appreciate that the developers of power shell have implemented command
syntax in a similar way
The command write-host can be used with no parameters, in which case
it will create a blank line of output(see Listing 2, page 6)
We can find options for write-host by typing help write-host (see
Listing 1: CoMmand Syntax
-
[-I
[-J
[-]
Listing 2: Hello world
PSM:、> write-host
PS M:\> write-host Hello, World
Hello world
Command Aliases
clear-host cls, clear
format-list A
get-childitem gci, Is, di
get-content gc, cat, type
get-location gl, pwd
get-member 8
remove-item ri. rm. rmdir. del erase rd
write-output write, echo
Table 1: Some Power Shell Command Aliases
Listing 3, page 7). For extra detail, add the - full"option: heLp-full write
3.1 Exercise
1. Type write-host"Hello World"and press the Enter key
2. Type write-host-foregroundcolor yellow"Hello World
3. Get Power Shell to print in blue text on a yellow background? clue
use the -backgroundcolor parameter
4. Type help cLear-host-onLine What happens?
4 Aliases
Many commands have aliases(see Table 1)and for those who have used DOS
or Unix these can be very familiar. Aliases are short forms of a command. So
for someone used to using the command pwd ty ping help pwd will indicate
that the underlying command is actually get-Location. However, if coning
from a Unix or DOS environment, typing the form you are familiar with
makes adopting powershell easier
Listing 3: Getting help for the write-host command
S M:\> help write-host
Write-host
SYNOPSIS
customized output to a hos
IYNT
Write-Host [[-Object J