0

我想通过 vbscript 添加一个管理单元,但在将管理单元添加到控制台时遇到问题。它将在 Windows 7 环境中运行。如果有人可以看看并指导我正确的方向,我将不胜感激。谢谢。

<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>
4

1 回答 1

1

“本地计算机\非管理员”只是系统为管理单元的特定配置提供的描述。在这种情况下,实际的管理单元名称是“组策略对象编辑器”。从而消除代码更改中的错误

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

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

不幸的是,这只会让您在 MMC 中设置“选择组策略对象”对话框。然后,您必须使用该对话框手动选择所需的配置。据我所知,没有办法为 Snapins.Add 提供参数来选择本地非管理员用户。

下面的代码将完全自动化设置管理单元的过程。但是,它对 SendKeys 的依赖使其非常脆弱。它在我的系统上运行,但很有可能您需要修改击键顺序和/或时间延迟以使其在您的系统上运行。一旦你让它工作,就不能保证它会继续这样做,因为当地条件是可变的,并且会极大地影响时间。

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}"
于 2015-05-20T00:11:55.980 回答