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
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
$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()
}
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.
No comments:
Post a Comment