3.1. The .NET Framework
Although the .NET Framework is invariably described in terms of C# and VB.NETtwo programming languages that rely on the infrastructure they provideit's not just for developers. There are a number of facets to the framework, including a
Common Language Runtime
(CLR), which provides a common set of functionality to all .NET applications and tools. Compilers for the .NET programming languages generate
managed code
, which is a set of instructions
understood
by the CLR. When it comes time to run the managed code, the CLR converts it into native executable instructions (suitable for the architecture of the machine) and actually runs it. This whole process is largely transparent to both the developer and the end
user
. To complement the runtime, there is a broad
Class Library
that contains all kinds of useful functions and data types.
Although MSH itself is written as managed code, the only important takeaway is that the .NET Framework redistributable is needed for any MSH work. However, for our purposes, there are two key aspects of the .NET Framework that we'll see in MSH time and again: strongly typed structured objects and the Class Library.
Most command-line administrative
shells
, such as
cmd.exe
, have just one or two data types: strings and integers. Creative use of strings and integers has resulted in some really clever scripts, but
interoperation
and portability between different processes is often bound by a strict contract such as the exact format of string. The .NET Framework defines not only a number of standard data types, including strings and integers, but also Booleans (true/false), floating point
numbers
, and arrays (an ordered collection of many items of the same type). We call
variables
that are declared with these types
strongly typed
, meaning that if a variable claims to be a double precision floating-point number, we can be confident that it really is a number and not a string or something else. There's no need to check that the data inside a floating-point number is actually in the correct format because the .NET Framework handles that.
In addition to these strongly typed variables, the .NET Framework introduces the idea of a
class
, which is a container that encapsulates a number of data types to represent something altogether more complex. Take a date and a time that could be represented by the string
03/29/2001 18:44:01
. This is fine in the simple case, but it
falls
apart when we want to compare one
hour
with another; then, we need to resort to string parsing. Now instead, imagine a
DateTime
class that has separate
properties
integers, in this casefor each of the elements of the string: year, month, day, hour, minute, and second. The class defines the structure of data; when it's
populated
with some real values, we call it an
object
.
For example, the output of
get-date
is a
DateTime
object. Checking
(get-date).Hour
is a lot more
convenient
than converting the 12th and 13th
characters
of a string to a number. Classes can also have
methods
, which perform some action on the data contained inside. Two examples from the
DateTime
class include
AddDays
, which shifts a date by a number of days (correctly observing month and year boundaries), and
ToUniversalTime
, which returns the same instant in UTC.
The Class Library provides a wide set of auxiliary functionality, much of which is just as useful to a script author as it is to a developer. Instead of having to use addition, subtraction, and many tests, the
DateTime
class lets us compare the difference between two dates with ease. Similar classes exist for text manipulation, URL parsing, low-level network communication, XML work, and many other common
tasks
. Given its broad scope, the Class Library is divided into
namespaces
logical groups of classesthat are summarized on MSDN at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/cpref_start.asp. Many of the built-in cmdlets are built on top of this infrastructure, exposing its
usefulness
but hiding the underlying plumbing from view.
Now, let's get back to MSH and see how all of this comes together.
|