Tuesday, May 12, 2009

Managing the Management Console Like a Manager

I forgot that you can mess around (or mess up) the MMC console via its COM API.  That means you can write scripts to create consoles and so forth.  Here's an example derived from a compilation of Google search results, but with the usual twist of posting it in three different languages: VBScript, KiXtart and PowerShell v2.0.

VBScript Example
consoleName = "C:\MyConsole.msc"

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(consoleName) Then
Wscript.Echo "console already exists"
Else
On Error Resume Next
Set objMMC = CreateObject("MMC20.Application")
If err.Number <> 0 Then
Wscript.Echo "an error occurred. unable to create mmc console"
Wscript.Quit(0)
End If
objMMC.Show
Set objDoc = objMMC.Document
objDoc.snapins.add("Active Directory Users and Computers")
objDoc.snapins.add("Active Directory Sites and Services")
objDoc.snapins.add("Active Directory Domains and Trusts")
objDoc.snapins.add("Group Policy Management")
objDoc.snapins.add("ADSI Edit")
objDoc.ActiveView.StatusBarText = "Pane 1|Pane 2|Pane 3"
objMMC.UserControl = 1
objDoc.Name = consoleName
objDoc.Save()
End If
Set fso = Nothing



KiXtart Example



Break ON
$consoleName = "C:\MyConsole.msc"

If Exist($consoleName)
? "info: console already exists"
Else
$objMMC = CreateObject("MMC20.Application")
If @error <> 0
? "an error occurred. unable to create mmc console"
Exit @error
EndIf
$objMMC.Show
$objDoc = $objMMC.Document
$nul = $objDoc.SnapIns.Add("Active Directory Users and Computers")
$nul = $objDoc.SnapIns.Add("Active Directory Sites and Services")
$nul = $objDoc.SnapIns.Add("Active Directory Domains and Trusts")
$objDoc.ActiveView.StatusBarText = "Pane 1|Pane 2|Pane 3"
$objMMC.UserControl = 1
$objDoc.Name = $consoleName
$objDoc.Save()
EndIf



PowerShell Example



$consoleName = "C:\MyConsole.msc"

if (test-path $consoleName) {
write-host "info: console already exists"
}
else {
try {
$objMMC = new-object -Com MMC20.Application
}
catch {
"an error occurred. unable to create an MMC console object"
break
}
$objDoc = $objMMC.Document
$objMMC.Show()
$x = $objDoc.SnapIns.Add("Active Directory Users and Computers")
$x = $objDoc.SnapIns.Add("Active Directory Sites and Services")
$x = $objDoc.SnapIns.Add("Active Directory Domains and Trusts")
$objDoc.ActiveView.StatusBarText = "Pane 1|Pane 2|Pane 3"
$objMMC.UserControl = 1
$objDoc.Name = $consoleName
$objDoc.Save()
}



Comments









If you run these on Windows Vista or Windows 7 (or corresponding "Server" platforms) be sure to run them with Administrative Rights.  That means when you launch CMD or PowerShell, you need to right-click and choose "Run as Administrator" or you will get errors/failures.  The PowerShell example in particular will act weird.  It just blows up and I cannot seem to trap an exception that indicates a permissions problem.



The only meaningful difference between these three is that VBScript requires that you grab the FileSystemObject in order to check for the existing file.  A small, but extra step.  The other two have that functionality built into the base environment.  I always wondered why FSO wasn't just merged into WSH.



As far as the MMC20.Application interface goes, I'm still having no luck gaining access to the feature that turns the "Description" bar on.  If anyone knows how to do this, please drop a feedback comment so it's shared with other readers.

Post a Comment