This section includes some commands to enable you to try out a few WPS
features. WPS has two modes, interactive mode and script mode, which
are covered separately.
First, you'll use WPS in interactive mode.
Start WPS. An empty WPS console window will display (see Figure
1.3). At first glance, you might not see much difference between it and the
traditional Windows console. However, there is much more power in WPS,
as you will soon see.
NOTE
Note that the letter case does not matter. WPS does not distinguish
between uppercase and lowercase letters in commandlet names. |
Figure 1.3 (click to enlarge)

Figure 1.4 (click to enlarge)

At the command prompt, type get-service i*. A list of all installed
services with a name that begins with the letter I on your computer will display (see Figure 1.5). This was your first use of a commandlet with
parameters.
Figure 1.5 (click to enlarge)

Type get- and then press the Tab key several times. You will see WPS
cycling through all commandlets that start with the verb get. Microsoft
calls this feature tab completion. Stop at Get-Eventlog. When you press
Enter, WPS prompts for a parameter called LogName (see Figure 1.6).
LogName is a required parameter. After typing Application and pressing
Return, you will see a long list of the current entries in your Application
event log.
Figure 1.6 (click to enlarge)

The last example in this section introduces you to the pipeline features
of WPS. Again, we want to list entries from a Windows event log, but this
time we want to get only some entries. The task is to get the most recent
ten events that apply to printing. Enter the following command, which
consists of three commandlets connected via pipes (see Figure 1.7):
Get-EventLog system|Where-Object{ $_.source -eq "print" }
|Select-Object -first 10
Note that WPS seems to get stuck for a few seconds after printing the
first ten entries. This is the correct behavior because the first commandlet (Get-EventLog) will receive all entries. The filtering is done by the subsequent
commandlets (Where-Object and Select-Object). Unfortunately,
Get-EventLog has no included filter mechanism.
Figure 1.7 (click to enlarge)

WPS in Script Mode
Now it's time to try out PowerShell in script mode and incorporate a WPS
script. A WPS script is a text file that includes commandlets/elements of
PowerShell Script Language (PSL). The script in this example creates a
new user account on your local computer.
Open Windows Notepad (or any other text editor) and enter the following
lines of script code (which consists of comments, variable declarations,
COM library calls, and shell output):
Listing 1.4 Create a User Account
### PowerShell Script
### Create local User Acount
# Variables
$Name = "Dr. Holger Schwichtenberg"
$Accountname = "HolgerSchwichtenberg"
$Description = "Author of this book / Website: www.windows-scripting.com"
$Password = "secret+123"
$Computer = "localhost"
"Creating User on Computer $Computer"
# Access to Container using the COM library "Active Directory Service Interface (ADSI)"
$Container = [ADSI] "WinNT://$Computer"
# Create User
$objUser = $Container.Create("user", $Accountname)
$objUser.Put("Fullname", $Name)
$objUser.Put("Description", $Description)
# Set Password
$objUser.SetPassword($Password)
# Save Changes
$objUser.SetInfo()
"User created: $Name"
Save the text file with the name createuser.ps1 into the directory
c:temp. Note that the file extension must be .ps1.
Now start WPS. Try to start the script by typing c:\temp\createuser.ps1. (You can use tab completion for the directory and filenames.)
This attempt will fail because script execution is, by default, not
allowed in WPS (see Figure 1.8). This is not a bug; it is a security feature.
(Remember the Love Letter worm for WSH?)
Figure 1.8 (click to enlarge)

For our first test, we will weaken the security a little bit (just a little).
We will allow scripts that reside on your local system to run. However,
scripts that come from network resources (including the Internet) will
need a digital signature from a trusted script author. Later in this book you
learn how to digitally sign WPS scripts. You also learn to restrict your system
to scripts that you or your colleagues have signed.
To allow the script to run, enter the following:
Set-ExecutionPolicy remotesigned
Then, start the script again (see Figure 1.9). Now you should see a
message that the user account has been created (see Figure 1.10).
Figure 1.9 (click to enlarge)

Figure 1.10 (click to enlarge)

 |
 |
| TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of . |
|
| |
All Rights Reserved, , TechTarget |
|
|
|
|