运行 .bat 时自动结束 .vbs

运行 .bat 时自动结束 .vbs

我有这个脚本我需要它在启动 .bat 后完成(自动结束或自动终止并关闭 wscript.exe)

On Error Resume next
If WScript.Arguments.Named.Exists("elevated") = False Then
      'Launch the script again as administrator
       CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ /elevated", "", "runas", 1
       WScript.Quit
Else
      'Change the working directory from the system32 folder back to the script's folder.
      Set oShell = CreateObject("WScript.Shell")
      oShell.CurrentDirectory = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)

End If

Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
set objshell = createobject("wscript.shell")

homedrive = objshell.ExpandEnvironmentStrings( "%HOMEDRIVE%" )

SCRIPT = homedrive & "\test\bar.bat"
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)

NewPath = objFSO.BuildPath(strFolder, SCRIPT)
set objshell = createobject("wscript.shell")

objshell.Run (script),0,True

失败的尝试:

  • 我尝试过此代码但它没有起作用
  • WScript.Quit(在脚本末尾)不起作用
  • 将“true”更改为“False”,但不起作用

有任何想法吗?

更新:主要问题是启动 .vbs 后 wscript.exe 仍在运行(可以在任务管理器中看到),并且我无法在 .vbs 或 .bat 中使用 cmd 命令(taskkill /f /im "wscript.exe" /t),因为 .bat 和 .vbs 已关闭

如果我的问题没有答案:如果有人能告诉我如何使用 Windows 原生识别的方法(无需安装依赖项)启动 .bat,我将不胜感激。请排除以下方法:

  1. 直接启动 .bat
  2. 使用快捷方式启动 .bat
  3. 将 .bat 转换为 .exe
  4. 使用 .vbs 启动 .bat

答案1

尝试这个:

On Error Resume next
Set fso = CreateObject("Scripting.FileSystemObject")
set objshell = createobject("wscript.shell")

homedrive = objshell.ExpandEnvironmentStrings( "%HOMEDRIVE%" )

SCRIPT = homedrive & "\test\bar.bat"

If WScript.Arguments.Named.Exists("elevated") = False Then
    'Launch the script again as administrator
    CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ /elevated", "", "runas", 1
    WScript.Quit
Else
    'Change the working directory from the system32 folder back to the script's folder.
    objShell.CurrentDirectory = fso.GetParentFolderName(WScript.ScriptFullName)
End If

objshell.Run (script),0,False

答案2

更改此行:

objshell.Run (script),1,True

到:

objshell.Run (script),1,False

这告诉 VB 不要等待命令返回。

https://www.vbsedit.com/html/6f28899c-d653-4555-8a59-49640b0e32ea.asp

相关内容