771
Chapter 30: Confi guring and Managing SQL Server with PowerShell
30
Cmdlets are frequently aliased. In other words, a different command can be entered to run
the cmdlet, rather than using its own name. For example, when browsing a directory, the
PowerShell cmdlet to view the contents of the current directory is Get-ChildItem. Users
of the DOS operating system (or cmd.exe on current windows systems) are familiar with
the dir command, and UNIX users are familiar with the ls command. These three com-
mands do the same thing, and both dir and ls are included with PowerShell as aliases of
the Get-ChildItem cmdlet.
A feature of UNIX shell scripting environments is the capability to “pipe” the results of
one command into another command’s input buffer. This is a feature of shell scripting that
makes it powerful — the ability to string multiple commands together to provide informa-
tion quickly. PowerShell provides this ability, but differs from UNIX shell scripting in that
the UNIX pipe sends text from one command to another, whereas PowerShell pipes objects
from one cmdlet to another. UNIX scripts must parse the text from one command using com-
mands such as grep, awk, and sed to format the text in a form that the next command
expects. PowerShell’s objects are understood by the receiving cmdlet, and no such parsing is
required.
You can start the PowerShell environment in a number of different ways:
■ Select the Start button ➪ type PowerShell, and then select the Windows
PowerShell ISE.
■ From the cmd.exe command line, the powershell_ise command launches the ISE
environment as well.
■ There’s also the old SQL Server wrapper called sqlps.exe, which should basically
be avoided.
The examples here are based on the standard powershell_ise.exe environment.
Normally, in the PowerShell environment, the user is informed of this by the prompt PS> at
the beginning of the line where the next command is to be entered.
Here’s an example using a series of cmdlets, piping the results of one to the next, and then
fi ltering the results to get useful information. In this set of commands, the get-process
command is used to return a set of all processes on the system, pipe the results into a sort
on the size of the workingset (or the amount of memory each process is using), in descend-
ing order. Then select only the fi rst 10, returning the biggest memory hogs on the system:
get-process | sort-object workingset -descending |
select-object -first 10
PowerShell is not case-sensitive. In addition, the alias sort could have been used instead
of the proper name of sort-object, and select could have been used instead of
select-object. This set of commands produces results like this:
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
637 82 163668 157312 1228 232.92 2132 sqlservr
535 80 120208 117256 1225 261.53 1344 sqlservr
c30.indd 771c30.indd 771 7/31/2012 9:46:21 AM7/31/2012 9:46:21 AM
http://www.it-ebooks.info