Vbscript 创建一个vbs脚本,等待特定文件运行、执行某些操作并等待终止

Vbscript 创建一个vbs脚本,等待特定文件运行、执行某些操作并等待终止,vbscript,Vbscript,这是我的第一个问题,所以不要对我太苛刻;) 我正在尝试创建一个等待特定进程运行的VBS(例如C:\Program Files\MyApp\AppName.exe),路径很重要:如果在其他地方启动了同名进程,它不应作出反应。如果它找到具有正确目录的目标进程,则应启动另一个vbs脚本,该脚本将以与启动相同的条件等待应用程序停止 以下是我现在接触到的内容(与此无关,它不起作用,但应该给出一个想法): 希望你能帮我写这段代码。我需要它用于各种流程,因此我将其称为“AppName”,以便更好地参考。请参阅

这是我的第一个问题,所以不要对我太苛刻;)

我正在尝试创建一个等待特定进程运行的VBS(例如C:\Program Files\MyApp\AppName.exe),路径很重要:如果在其他地方启动了同名进程,它不应作出反应。如果它找到具有正确目录的目标进程,则应启动另一个vbs脚本,该脚本将以与启动相同的条件等待应用程序停止

以下是我现在接触到的内容(与此无关,它不起作用,但应该给出一个想法):


希望你能帮我写这段代码。我需要它用于各种流程,因此我将其称为“AppName”,以便更好地参考。

请参阅。您有Win32_ProcessStopTrace、Win32_ProcessStartTrace和Win32_ProcessStartStopTrace。这并不能解决我的问题,因为脚本还应该使用非管理员权限,并且仅针对特定的目录路径,应该提供关于逐路径操作的想法。通过一点调整,这可以用来监控do while循环中的流程,并在满足if条件(使用if process count<0 check)时执行所需的操作。我当前正在工作,否则我会用适当的示例提供完整答案。请参阅。您有Win32_ProcessStopTrace、Win32_ProcessStartTrace和Win32_ProcessStartStopTrace。这并不能解决我的问题,因为脚本还应该使用非管理员权限,并且仅针对特定的目录路径,应该提供关于逐路径操作的想法。通过一点调整,这可以用来监控do while循环中的流程,并在满足if条件(使用if process count<0 check)时执行所需的操作。我当前正在工作,否则,我会用适当的示例提供完整的答案。
Option Explicit
'On Error Resume Next


Dim wshshell, objShell, Systemdrive, Username, AppData, oFSO, oShell,  RIGHTHERE, strComputer, objWMI, ColOSs, OS, OSType, service, AppName, Process, AppName2, objReg, Path, PathCompare, colItems, objItem, ObjWMIService

SET WshShell = CreateObject ("WScript.Shell")
RIGHTHERE = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName))


set service = GetObject ("winmgmts:")

AppName = "AppName.exe"

Function IsAppRunning(AppName)
    for Each Process in Service.Instancesof("Win32_Process")
        If UCase(Process.Name) = UCase(AppName) then
Path = Process.ExecutablePath
' msgbox "Paths name is...: " & Path
PathCompare=RIGHTHERE & AppName
' msgbox RIGHTHERE & AppName
        If UCase(Path) = UCase(PathCompare) then
' Process "AppName.exe" found with same path as VBS
' msgbox "Paths are identical!"
IsAppRunning = True

Set objShell = CreateObject("WScript.Shell")
objShell.Run "ReportMe.vbs",1,True


Exit function
' VBS is automatically terminated here, no idea why
else
' Process name is "AppName.exe", but its file path is not the same as that of the VBS
' msgbox "No match - Go to next process!"  
end if 
else
' here the found process is not even called "AppName.exe" - skip tacitly
        End If  
next   
Exit function
    IsAppRunning = False
End Function


Do while IsAppRunning(AppName)
    Wscript.Sleep(1000) 'wait miliseconds
msgbox "Prozessis still running/not running (depends on wait for start/stop)!"
Loop


Wscript.quit