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:
- PowerShell 1.0 installed (which requires .NET Framework 2.0)
- Windows XP or Server 2003 (not tested on 2000)
- 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)
- 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.
- 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:
- Create a folder to contain the shortcuts. We'll use "MMC" here
- Within "MMC" create a folder called "_script"
- Within "MMC\_script" create the following:
- 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.
- A text file called "CreateMMC.ps1" with the following:
- 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.
- Double-click the shortcut within "MMC" and see how it works!
$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()
}
"PS c:\Set-ExecutionPolicy Unrestricted"