772
Part V: Enterprise Data Management
562 18 99972 77364 357 457.14 3580 Ssms
598 11 52048 50352 179 57.86 4012 powershell
308 73 61612 45740 1155 156.54 728 sqlservr
602 17 57452 37956 255 298.60 1400 Reporting
ServicesService
494 10 26636 33144 155 5.93 3308 SQLPS
713 46 36704 27984 210 241.31 1264 msmdsrv
1011 42 12872 19556 80 144.29 808 svchost
158 4 12248 13272 104 2.22 1204 MsDtsSrvr
PowerShell variables are declared by preceding the variable name with the dollar sign ($) char-
acter. Variables are actually objects of a particular type, and that data type can be created by
preceding the variable name with the data type in brackets ([]). For example, a variable called
$counter that’s an integer object can be created and set to a value of 7 as follows:
PS> $counter = 7
However, a string object variable with the value of '7' can be created as follows:
PS> [string] $counter = '7'
Variables can also contain objects or collections. Collections are just a group of objects,
such as an array. It’s easy to create a collection just by assigning a list of values to a
variable, like this:
PS> $stuff = 1,2,4,6,8
The list of numbers is grouped into a collection of integer objects and placed in the variable
$stuff. Individual elements within the collection can be accessed by using their ordinal
number:
PS> $stuff[2]
4
PS>
Addressing individual elements of an array is nice, but the power of collections is real-
ized by iterating through the members of the collection. PowerShell offers two versions of
foreach logic; the fi rst is a cmdlet to which a collection is piped, like this:
PS> $stuff | foreach-object {write-output $_}
1
2
4
6
8
The variable $_ is defi ned as the current object in the set of objects the cmdlet is iterating
through. The other version is the foreach language element, which enables naming of the
member:
PS> foreach ($thing in $stuff) {write-output $thing}
1
c30.indd 772c30.indd 772 7/31/2012 9:46:21 AM7/31/2012 9:46:21 AM
http://www.it-ebooks.info