Sunday, February 4, 2007

Abbreviated MMC Snap-ins

As a system administrator, the most frequent windows tools I use are the MMC administrative snap-ins.
Whether locally or for remote computers, snap-ins are all around. You can use them to administer networks, computers, services and other system related tasks. MMC snap-ins can be launched either by clicking on shortcut files or by running them in command line.
The Run dialog (Start > Run) is another place to run them. Most snap-ins support the /computer=computer_name to connect the snap-in to a remote computer.

Personally, the shortest way I use (3 steps) to run snap-ins, is to press WIN+R to invoke the run dialog, type the .msc file name (including extension) and press ENTER. In the endless effort to shorten up the typing process or mouse click to launch applications, I wanted to be able to run .msc files from the run dialog without typing their extensions.

I did some tests including adding the "*.MSC" extension to the windows system %PATHEXT% environment variable and tried to run "eventvew", or some other snap-ins... it didn't work. The "mgmt" suffix for some snap-ins (dnsmgmt.msc,dhcpmgmt.msc etc) was also targeted, on a daily basis its too long. I need to launch snap-ins with abbreviated names, using my own aliases. The solution was to write a script that accept an argument (.msc alias) and fires the relevant snap-in.

Here is the result. Now, system administration with snap-ins, from within the blue console, is only an alias away. You can call the function in various ways:

 

# with full parameter names msc -snapin dns -machine serverName
# with abbreviated parameter names msc -s dns -m serverIP
# with possitioned parameters msc events serverName Don't forget to include it in your profile or just dot-source it.


function Invoke-MMCSnapIn{    
     param(        
         [string]$snapin,
         [string]$machine=$env:computername
     )     
     $ErrorActionPreference = 0 # SilentlyContinue     
     if(!$snapin) {
         write-warning "Missing snap-in name"
         return
     } else{
         switch($snapin.tolower()){
             "svcs" {$snapin="services"}
             "events" {$snapin="eventvwr"}
             "dns" {$snapin="dnsmgmt"}
             "dhcp" {$snapin="dhcpmgmt"}
             "device" {$snapin="devmgmt"}
             "comp" {$snapin="compmgmt"}
             default {write-warning "No snap-in defined"}
         }     
     }        
     if($machine -eq $env:computername) {iex "$snapin.msc"}
     else{iex "$snapin.msc /computer=$machine"}
}
Set-Alias msc Invoke-MMCSnapIn

 

$hay

No comments: