Scripting MMC with vbscript

1.8k views Asked by At

I would like to add a snap in via vbscript and I have been having a problem getting the snap in to add to the console. It will be run in a Windows 7 environment. If someone could have a look see and direct me in the right direction I would be most grateful. Thanks.

<code>

'Elevated privileges start
'Start of UAC workaround code
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If WScript.Arguments.length =0 Then
    Set objShell = CreateObject("Shell.Application")
    objShell.ShellExecute "wscript.exe", Chr(34) & _
    WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else


consoleName = "C:\Burnett.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("Local Computer\Non-Administrators")
    if err then
    'Trap the error just after the statement where an error/exception can occur and handle it elegantly
        msgbox("Snap-in Not found")  
        err.clear
    end if
    objDoc.ActiveView.StatusBarText = "Pane 1|Pane 2|Pane 3"
    objMMC.UserControl = 1
    objDoc.Name = consoleName
    objDoc.Save()
End If

Set fso = Nothing


End If 

</code>
1

There are 1 answers

0
Rob Boylan On

"Local Computer\Non-Administrators" is just a system-supplied description for the particular configuration of a snap-in. In this case, the actual snap-in name is "Group Policy Object Editor". Thus to eliminate the error in the code change

objDoc.snapins.add("Local Computer\Non-Administrators")

to

objDoc.snapins.add("Group Policy Object Editor")

Unfortunately, this will only get you as far as MMC putting up a "Select Group Policy Object" dialog. You will then have to manually select the configuration you need using that dialog. As far as I can tell there is no way to supply Snapins.Add with the parameters to select the local non-admin users.

The code below will fully automate the process of setting up the snap-in. However, its reliance on SendKeys makes it extremely brittle. It worked on my system, but there's a good chance you'll need to modify the sequence of key strokes and/or the timing delays to make it work on your system. And once you get it working, there's no guarantee it will continue to do so as local conditions are mutable and can greatly effect the timing.

option explicit

if WScript.Arguments.Named.Exists("elevated") = false then 
  'Launch the script again with UAC permissions
  CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ /elevated", "", "runas", 1
  WScript.Quit
end if

Dim mmc : set mmc = WScript.CreateObject("MMC20.Application")
mmc.Show
mmc.UserControl = 1   'to keep MMC open

Dim oShell : set oShell = WScript.CreateObject("Wscript.Shell")
oShell.AppActivate "Console1"
WScript.Sleep 200
oShell.SendKeys "%f"
WScript.Sleep 200
oShell.SendKeys "m"
WScript.Sleep 400
oShell.SendKeys "group{TAB}{ENTER}"
WScript.Sleep 1000
oShell.SendKeys "{TAB}{ENTER}"
WScript.Sleep 1000
oShell.SendKeys "{TAB}{TAB}{TAB}{RIGHT}{TAB}Non{ENTER}" 
WScript.Sleep 1000
oShell.SendKeys "{TAB}{TAB}{ENTER}"
WScript.Sleep 1000
oShell.SendKeys "{TAB}{TAB}{TAB}{TAB}{ENTER}"