は、スクリプトを停止するための質問をするためにそのような何かを試すことができます。
Option Explicit
Dim Title,Ws
Title = "Ask a question to stop the script !"
Set Ws=CreateObject("Wscript.shell")
Do
Ws.SendKeys "{SCROLLLOCK}"
WScript.sleep 10000
Call Ask_Question()
Loop
Sub Ask_Question()
Dim Answer
Answer=MsgBox("Did you want to stop this script ?"_
& vbcr & "(Yes/No) ?",vbQuestion+vbYesNo,Title)
If Answer=vbYes Then
Wscript.Quit(0)
Else
Exit Sub
End If
End Sub
編集12時53
@ 19/08/2016ただ、一般的に例:
あなたはどのプログラムを監視したのかわからないので、Notepad.exe
と計画しています。 このスクリプトは、progr am Notepad.exe
が実行中であるかどうか そうでない場合は、スクリプトを停止するかどうかを尋ねます。
Option Explicit
Dim ProcessPath,WshShell
ProcessPath = "%Windir%\System32\Notepad.exe"
Set WshShell = CreateObject("WScript.Shell")
If AppPrevInstance() Then
MsgBox "There is an existing proceeding !" & VbCrLF &_
CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"
WScript.Quit
Else
Do
Call Main()
Pause(10) ' Pause 10 seconds
If CheckProcess(DblQuote(ProcessPath)) = False Then
Call Ask_Question()
End If
Loop
End If
'**************************************************************************
Function CheckProcess(ProcessPath)
Dim strComputer,objWMIService,colProcesses,Tab,ProcessName
strComputer = "."
Tab = Split(ProcessPath,"\")
ProcessName = Tab(UBound(Tab))
ProcessName = Replace(ProcessName,Chr(34),"")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '"& ProcessName & "'")
If colProcesses.Count = 0 Then
CheckProcess = False
Else
CheckProcess = True
End if
End Function
'**************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************************************
Sub Pause(Secs)
Wscript.Sleep(Secs * 1000)
End Sub
'**************************************************************************
Function AppPrevInstance()
With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
" AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")
AppPrevInstance = (.Count > 1)
End With
End With
End Function
'***************************************************************************
Function CommandLineLike(ProcessPath)
ProcessPath = Replace(ProcessPath, "\", "\\")
CommandLineLike = "'%" & ProcessPath & "%'"
End Function
'****************************************************************************
Sub Main()
WshShell.SendKeys "{SCROLLLOCK}"
End Sub
'****************************************************************************
Sub Ask_Question()
Dim Answer,Title
Title = "Ask a question to stop the script !"
Answer=MsgBox("Did you want to stop this script ?"_
& vbcr & "(Yes/No) ?",vbQuestion+vbYesNo,Title)
If Answer=vbYes Then
Wscript.Quit(0)
Else
Exit Sub
End If
End Sub
'****************************************************************************
手動で停止するまでスクリプトを実行する必要がありました。時間間隔の後に表示されるMsgBoxは、スクリプトが書き込まれた目的を削除します。 – Ansh
いくつかの文章を追加しました – prizm1