Monday, October 03, 2005

Customising Monad via Profile files

Monad, or specifically, msh.exe, implements a set of cmdlets, providers etc. These are pretty basic, but you can extend and customise Monad easily through the use of profile scripts. There are up to four profile files which are loaded when you run MSH.exe:

1. "All Users\Documents"\MSH\profile.msh
2. "All Users\Documents"\MSH\<shellid>_profile.msh
3. "My Documents"\msh\profile.msh
4. "My Documents"\msh\<shellid>_profile.msh

The "All Users documents" is dependent on where you installed the OS, specifically %SYSTEMDRIVE%\Documents and Settings\All Users\Documents\MSH. Likewise, "My documents" is at %USERPROFILE%\My Documents\MSH.

The <shellid> field in the 2nd and 4th profile, is a name generated by Monad to uniquely identify a specific shell. The default name, which is contained in the $Shellid variable, is "Microsoft.Management.Automation.msh". At first sight this is a little odd, but there is a logic to it. In the current builds of MSH, you have to use the make-shell command to extend monad with new cmdlets and providers. By doing so, you create a new executable (mymsh.exe for example), which would have a different name (e.g. Microsoft.Management.Automation.mymsh). This means you can have shell specific profile files - for example to provide some functions on top of custom developed cmdlets.

In the early builds of Monad, I tended to just customise the "my documents\profile.msh". But more recently, I've created my own profile (profile.tfl.msh) and I run this from within the profile.msh. That way, when I install newer versions of Monad, I don't have to worry about losing my customised settings - I just add one extra line to the default profile.msh and all my customisations are included.

One small thing about executing private scripts from within a profile.msh. Whenever you run a script, by default any variables, functions, etc created by that script are recycled when the script is completed. You can do that's called "dot-sourcing" to enable the customisations in our private profile script to be added to your current context. Whenever you run a script and start the line with a dot (followed by a space), the results of the script are added to your current scope. Thus typing "profile.tfl.msh" results in no functions, variables etc being available once the script runs. But by typing ". profile.tfl.msh", my Monad customisations are available in the msh.exe shell. The four profile files noted above are all dot-sourced.

These features are some what more complex than you have in CMD.EXE (and most Unix shells). But they do provide for considerable flexibility in terms of configuring and extending MSH. They are worth playing around with.

[On The Plane - via Email]

No comments: