Wednesday, October 3, 2007

Powershell script to create custom "compmgmt.msc" shortcuts

I have several hundred servers I manage. I like to keep a folder on my "Quick Launch" bar called "Computers" with custom shortcuts to "compmgmt.msc" contained within, broken down by environment.

This script is possible because of a feature of MMC: you can call "services.msc" or "compmgmt.msc" or probably all of them with the switch "/computer:computername" to connect to a remote computer immediately instead of having to right-click and choose "connect" once launched.

Prerequisites:
  1. PowerShell 1.0 installed (which requires .NET Framework 2.0)
  2. Windows XP or Server 2003 (not tested on 2000)
What this script does:
  1. It creates subfolders organized as specified in a comma-delimited file to create the shortcuts in, so I can have one for each environment we have (QA, Staging, Production, etc)
  2. It deletes existing shortcut files within the folder structure before creating new ones. Note, however, I do not delete all shortcut files. If you remove an entry from the CSV file, it will remain unless you delete the subfolders manually.
Script files
  • You can download everything mentioned in this article by clicking here: CreateMMC_7.10.3.rar
  • Note: the above archive contains an example shortcut hard-coded based on my computer. You'll want to edit it or create a new one.

Steps:
  1. Create a folder to contain the shortcuts. We'll use "MMC" here
  2. Within "MMC" create a folder called "_script"
  3. Within "MMC\_script" create the following:
    1. A comma-delimited file called "list.csv" with the following column headings: "ServerName", "IP", "Directory" (first row should read: "ServerName,IP,Directory" if using a text-editor). For each row, fill in the proper values for each server. "IP" can be either the IP address or FQDN. "Directory" refers to the subfolder of "MMC" that will be created to hold the shortcut for this server.
    2. A text file called "CreateMMC.ps1" with the following:

    3. $List = Import-CSV LIST.CSV
      $sh = new-object -com "WScript.Shell"
      $TargetPath = "%SystemRoot%\system32\compmgmt.msc"

      ForEach($Entry in $List) {
      # Prepend the destination directory info
      $Dir = "..\" + $Entry.Directory

      # Create new folder
      New-Item -Path $Dir -ItemType Directory -Force

      # Build the file name and properties
      $FileName = $Dir + "\" + $Entry.ServerName + ".lnk"
      $Arguments = "/computer:" + $Entry.IP

      # Remove the old file
      Remove-Item $FileName -Force

      # Create shortcut
      $shortcut = $sh.CreateShortcut($FileName)
      $shortcut.TargetPath = $TargetPath
      $shortcut.Arguments = $Arguments
      $shortcut.Save()

      }

  4. Create a shortcut of the script and save it under "MMC" taking care that the "Start In" folder is still "MMC\_script". This is necessary if you want to just be able to click the file and have it create / update your shortcut files. Otherwise, PowerShell will complain about not finding the script and I prefer to not hardcode paths within the script.

  5. Double-click the shortcut within "MMC" and see how it works!
Note, you may have to enable script execution. Powershell by default does not automatically execute scripts. Microsoft, being overly paranoid after .vbs files being used as trojan and virus entry points, set the default execution policy for Powershell scripts to "deny". To enable scripts, you'll need to either sign the scripts with a trusted certificate or, as I do, just enable script execution:
"PS c:\Set-ExecutionPolicy Unrestricted"

0 comments: