OOPS with Microsoft Shell

author-image
CIOL Bureau
Updated On
New Update

Vinod Unny

Advertisment

When we look at the upcoming command shell from Microsoft called MSH (Microsoft Shell). It allows power users to utilize their machines and create new ways of managing the system better. Unlike other shells such as the DOS prompt or the Linux BASH prompt which uses procedural logic in any scripting, MSH uses a different methodology.

Being built on managed code (read .NET), MSH uses a completely object-oriented philosophy as its base. Microsoft has invented a new language for scripting MSH. It uses a combination of familiar shell programming constructs from other standard shells as well as uses the OOPS concepts that are now inherent in this shell. MSH also has a concept of CmdLets (pronounced Command-Lets), equivalents of small commands, which can be combined to do more tasks. Let's look at how the new shell works. Every shell has a way to create variables that can be used in different ways. MSH follows the same idea. Here are some ways you can create different types of variables in MSH.

MSH> $string = “Hello World”
MSH> $number = 3.141
MSH> $normalarray = 10,9,8,7,6,5
MSH> $associativerray = @{no = 0; yes = 1; maybe = 100}

Advertisment

MSH> echo $string
Hello World
MSH> echo $number
3.141
MSH> echo $normalarray
10
9
8
7
6
5
MSH> echo $associativearray
Key Value
--- -----
no 0
yes 1
maybe 100

Direct Hit!
Applies to: System administrators, power users and developers
USP: Insight into object-oriented shell scripting using the .NET framework
Primary Link: msdn.microsoft.com
Google keywords: Monad, MSH, CmdLet

Normal operation such as +,-,/,*,+=,-=,/=,*= are all allowed amongst variables. Of course, there are rules as to what will happen depending on the variables involved. However, the interesting part comes when you can start assigning data types to the variables. For instance, you can specify a set of different MSH data types as well as standard .NET types to the variables.

Advertisment

MSH> $number = 10
MSH> $contact = “VinodGurgaon
MSH> $independenceday = “08/15/1947”
MSH> $newyear = New-Object System.DateTime 2006,1,1

Since all variables in MSH are actually objects, normal variable value substitution becomes even more powerful than before. Take a look at the difference in the two examples below.

MSH> $folder = “C:\Windows”
MSH> dir $folder
Directory: FileSystem::C:\Windows

Advertisment

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- Apr 05 2004 64512 agrsmdel.exe
-a--- Oct 28 15:37 88363 agrsmmsg.exe
-a--- Aug 04 05:00 1272 Blue Lace 16.bmp
...

MSH> $process = Get-Process Notepad
MSH> echo $process.Name
Notepad.exe

In the second example, the variable '$process' is an object that contains different properties-one of them being 'Name'. 
You can also access methods of the objects in MSH in the same manner. For instance, take a look at the examples that follow.

Advertisment

MSH> $str = “My name is Anthony Gonzalves”
MSH> echo $str.SubString(11, 3)
Ant
MSH> echo $str.ToUpper()
MY NAME IS ANTHONY GONZALVES
MSH> echo $str.StartsWith(“Test”)
False
MSH> echo $str.StartsWith(“My”)
True
MSH> echo $str.Split(“ ”)
My
name
is
Anthony
Gonzalves

You can even call static methods on the data type by prepending the method with '::'. For instance,

MSH> ::Compare(“TEST”, “NEST”)
-1
MSH> ::Today
06/09/2006

Advertisment

Finally, you can create functions that let you perform a particular task in the following manner. These functions can be written at the command line (valid for that session) or put within an .MSH file (like a .sh or .bat file).

MSH> function percentage { 
>> $value = $args<0> * 100 / $args<1>
>> $result = “The percentage is: “ + $value + “%”
>> }
MSH> percentage 50 100
The percentage is: 50%

You can also specify the parameters that a function should have, with their data type and their optional defaults. For instance,

Advertisment

MSH> function area {
>> param ( $length = 0, $width = 0, $height = 0)
>> $result = $length * $width * $height 
>> “The area is: ” + $result.ToString()}
MSH> area
The area is: 0
MSH> area 5, 10, 15
The area is: 750

Thus, an object oriented powerful programming model for the new MSH lets you create scripts and functions that can work with the entire .NET object model at the command line itself. It is surmised that this new shell will be released for Win XP and higher after the release of Windows Vista. It will also be one of the new remote-management options for the next version of MS Exchange.

Source: PCQuest

tech-news