如何通过 Batch-Vbscript 混合脚本获取新生成进程的 Window 标题?

How to get Window title of newly spawned process thro' Batch-Vbscript hybrid script?

我正在尝试尽可能自动化组策略编辑过程。

我有以下脚本来生成 gpedit.msc 进程,但它 window 一打开就失去焦点:

FINDSTR /E "'VbsCode" %~f0 > %temp%\~temp.vbs
CSCRIPT //NOLOGO %temp%\~temp.vbs
Sub GPEditOptions 'VbsCode
    On Error Resume Next 'VbsCode
    Set WshShell = WScript.CreateObject("WScript.shell") 'VbsCode
    WshShell.Visible = False 'VbsCode
    WshShell.Run "gpedit.msc",0 'VbsCode
    :: WshShell.AppActivate "Local Group Policy Editor" 'VbsCode
End Sub 'VbsCode
GPEditOptions 'VbsCode
:: WScript.Quit 0 'VbsCode

如何 AppActivate 新生成的 gpedit.msc 进程打开的 window?具体如何知道打开的 window 的 name/title 是什么? “本地组...编辑器”不起作用。

我想我终于想通了,如何解决这个问题。有不同类型的进程。

在这种情况下,我首先需要 select Microsoft Management Console window,因为它是产生实际“本地组策略编辑器”子进程的父进程。

所以这段代码通过预先发送大量密钥来完成 select 以字母“W”开头的第一个 Windows 组件的工作,是的,您确实需要管理员提升才能正确 selectgpedit.mscwindow中的选项离子:

@echo off
net file 1>nul 2>nul
if not '%errorlevel%' == '0' (
    powershell Start-Process -FilePath "%0" -ArgumentList "%cd%" -verb runas >nul 2>&1
    exit /b
)
cd /d %1

FINDSTR /E "'VbsCode" %~f0 > %temp%\~temp.vbs
CSCRIPT //NOLOGO %temp%\~temp.vbs
Sub GPEditOptions 'VbsCode
    On Error Resume Next 'VbsCode
    Set WshShell = WScript.CreateObject("WScript.shell") 'VbsCode
    WshShell.Visible = False 'VbsCode
    WshShell.Run "gpedit.msc",0 'VbsCode
    WScript.Sleep 500 : WshShell.AppActivate "Microsoft Management Console" 'VbsCode
    WScript.Sleep 500 : WshShell.AppActivate "Local Group Policy Editor" 'VbsCode
    WScript.Sleep 500 : WshShell.sendKeys "% x{TAB}{ENTER}" 'VbsCode
    WScript.Sleep 500 : WshShell.sendKeys "{TAB}{TAB}{TAB}{TAB}" 'VbsCode
    WScript.Sleep 500 : WshShell.sendKeys "{DOWN}{DOWN}{ENTER}" 'VbsCode
    WScript.Sleep 500 : WshShell.sendKeys "{TAB}{TAB}{TAB}{TAB}" 'VbsCode
    WScript.Sleep 500 : WshShell.sendKeys "{DOWN}{DOWN}{DOWN}{DOWN}" 'VbsCode
    WScript.Sleep 500 : WshShell.sendKeys "{DOWN}{DOWN}{ENTER}" 'VbsCode
    WScript.Sleep 500 : WshShell.sendKeys "{TAB}{TAB}{TAB}{TAB}{W}" 'VbsCode
End Sub 'VbsCode
GPEditOptions 'VbsCode
WScript.Quit 0 'VbsCode

希望这对遇到类似问题的人有所帮助。

无需发送一堆密钥并获得组策略编辑器 window 称号。

实际上每个组策略设置都有等效的注册表项。并且可以从 VBScript 轻松编辑注册表。要查找组策略设置的等效注册表项:

  • 从 SysInternals 下载名为 Process Monitor 的工具。
  • 运行 它并单击过滤器 > 过滤器。
  • 现在创建两个过滤器,例如:"Process name" - "is" - "mmc.exe" - then "include""Operation" - "is" - "RegSetValue" - then "Include"
  • 现在编辑组策略设置,注册表项将出现在进程监视器中。

以及在 VBScript 中编辑注册表的函数:

Function RegSetValue(regkey,value)
  Dim WshShell
  Set WshShell = CreateObject("Wscript.Shell")
  WshShell.RegWrite(regkey,value)
End Function