2015/04/23

久々に・・・

・・・新作スクリプト。


って、内容としては、プロセスを プロセス名指定で終了させるだけのありがち過ぎるモノ。



まぁ、MS-DOSコマンドの Killコマンドの代替、Taskkillコマンドを利用すれば同じなんだが、
それだとコマンドプロンプトが表示されてスマートじゃないってコトで軽く作成してみた。

要望主へのTipsしつつの制作だったから、iniからの読み込みで とかはやってない。
コマンドラインパラメータで、ある程度の数のプロセスを任意に指定して終了させるだけに絞った。


・アップデートリンク
■ 概要 ■
書式)
tProcess.vbs {開始までの待ち時間(ms)} プロセス名 {プロセス名} {プロセス名}・・・

ないし、

tProcess.vbe {開始までの待ち時間(ms)} プロセス名 {プロセス名} {プロセス名}・・・
▲ このウチ、 パラメータに関しては、
・カッコ{}の箇所は省略可。
・先頭引数(最初のパラメータ)が 数字として認識出来るモノなら 停止処理開始までの待ち時間として扱われる。
・プロセス名は 大文字/小文字も完全に一致している必要がある。
・・・と、なっている。
■ 用法 ■
tProcess.vbsファイルを任意の運用フォルダにレイアウトしてショートカットを作成し、
そのショートカットのプロパティで [ショートカット]タブの[リンク先]枠を・・・
"C:\Program Files\Okitsunesama\Scripts\ProcessTerminator\tProcess.vbs" 1000 notepad.exe "全角文字ファイル.exe"
▲ このように パラメータを半角スペースで区切りつつ、全角文字やスペースがファイル名に含まれる場合は ダブルクォーテーションで囲んで追記し準備完了。 (この例は .vbsファイルの場合) ショートカットパラメーター文字数上限以下であれば、ショートカットの[リンク先]枠内や コマンドラインパラメーターとして並べた対象の順序で、処理が実行される。 用途別にショートカットの名前を変えれば、スクリプト本体1つで、複数の処理セットを設置しておける。

■ スクリプト本体 ■
・tProcess.vbs
'***** ↓↓↓ScriptTitle↓↓↓ ********************************************************* 
    ' tProcess.vbs - ProcessTerminator 
    ' - Created by LazwardFox - 

    ' Release 20150423 035751 
    ' Update 20150423 025152 処理開始までの待機時間指定を追加。 
    ' αRelease 20150423 023704 
    ' DevStart 20150423 021040 


'***** ↓↓↓ Memo ↓↓↓ ************************************************************** 

    '  


'***** ↓↓↓ ObjectDecralations ↓↓↓ ************************************************ 
    Option Explicit

    Dim My, Parameters 
    Set My = WScript
    With My
        Set Parameters = .Arguments 'パラメーター取得 
        If Parameters.Count <= 0 Then 'パラメータなし起動の無効化 誤操作による起動を防止 
            .Quit
        End If
    End With


'***** ↓↓↓Decralations↓↓↓ ******************************************************** 
    Dim Pc, strTarget, objProcS, objProc


'***** ↓↓↓MainRoutine↓↓↓ ********************************************************* 
    On Error Resume Next

    If IsNumeric(Parameters(0)) Then
        ' 先頭パラメータが数字だったら待機時間指定と見做す。 
        If Parameters.Count > 1 Then
            My.Sleep Parameters(0)
            Pc = 0
        Else
            My.Quit
        End If
    Else
        ' 先頭パラメータが数字以外だったら 停止処理対象文字列と見做す。 
        Pc = 1
    End If

    For Each strTarget in Parameters
        If Pc = 0 Then ' 先頭パラメータのスキップ。 
            Pc = 1
        Else
            Set objProcS = GetObject("winmgmts:").InstancesOf("win32_process")
            For Each objProc In objProcS
                If CStr(objProc.Name) = strTarget Then
                    objProc.Terminate
                    My.Sleep 300 ' 処理待ち。 
                End If
            Next
            Set objProcS = Nothing
        End If
    Next

    Set My = Nothing
◆ ついでに・・・
自己解凍zipで おきつね仕様のインストーラも作ってみた。
tProcessInstaller.exe 20150423版
自分でvbsファイルを設置するのが面倒な向きに・・・ 注) このインストーラでデスクトップに出来るアイコンは あくまでテンプレートで、そのままでは機能しないようにしてあります。 そのショートカットをコピーし、[プロパティ]画面 [ショートカット]タブ [リンク先]枠に 任意で、 処理開始までの待機時間や、対象プロセス名 を 追記して 利用して下さい。

・・・機能追加。 ■ 概要 ■
基本的な用法や記述ルールは ほぼ同じまま。 変わったのは・・・
◆ 先頭にしか指定出来なかった待機時間を、プロセスの前後に任意で設置出来る様になった。

◆ プロセス名として、
■ フルパス指定で先頭に + つけると、実行開始対象として扱われます。

▼ msi AfterburnerRemoteServerを開始する例。
"C:\~省略~\tProcess.vbe" "+C:\Program Files (x86)\~省略~\MSIAfterburnerRemoteServer.exe"
 ファイル名記述ルールの関係で コチラの利用にはダブルクォーテーション囲む事が必須になる。 ■ プロセス終了対象に限り、ワイルドカード符号 * で、先頭文字列一致に暫定対応。 ▼ ワイルドカード指定で BlueStacksを終了させる例。
"C:\Program Files\Okitsunesama\Scripts\ProcessTerminator\tProcess.vbe" HD-*
・・・拡張子識別は憑けていない、あくまで先頭文字列一致のみ。
◆ これら機能追加により・・・
■ 開始/終了の対象 と 待機時間 を 複数列挙して利用出来るようになっています。
▼ 待機時間指定ナシで、BlueStacksを終了させ、msi AfterburnerRemoteServerを開始する例。
"C:\~省略~\tProcess.vbe" HD-* "+C:\Program Files (x86)\~省略~\MSIAfterburnerRemoteServer.exe"
  ▼ 開始待機時間1秒の後、BlueStacksを終了させつつ 6秒待機し、
"C:\~省略~\tProcess.vbe" 1000 HD-* 6000 "+C:\Program Files (x86)\~省略~RemoteServer.exe" 5000
・・・ msi AfterburnerRemoteServerを開始しつつ 5秒待機する例。
以下に そのスクリプトソースを・・・ ■ スクリプト本体 ■
・tProcess.vbs
'***** ↓↓↓ScriptTitle↓↓↓ ********************************************************* 
    ' tProcess.vbs - ProcessTerminator 
    ' - Created by LazwardFox - 

    ' Update  
    ' Update 20150427 072818 - 端折っていた分岐を加筆。 
    ' Update 20150426 235042 - 指定処理毎の待機時間指定可能に。 
    ' Update 20150426 224446 
    '         - 先頭一致検索でのプロセス停止と、プログラム実行開始指定を可能に。 
    ' Release 20150423 035751  
    ' Update 20150423 033144 - 関数名間違い/変数宣言忘れ/記述ミスを訂正。 
    ' Update 20150423 025152 - 処理開始までの待機時間指定を追加。 
    ' αRelease 20150423 023704  
    ' DevStart 20150423 021040  


'***** ↓↓↓ Memo ↓↓↓ ************************************************************** 

    '  


'***** ↓↓↓ ObjectDecralations ↓↓↓ ************************************************ 
    Option Explicit

    Dim My, Parameters, MySh, Fs
    Set My = WScript
    With My
        Set Parameters = .Arguments 'パラメーター取得 
        If Parameters.Count <= 0 Then 'パラメータなし起動の無効化 誤操作による起動を防止 
            .Quit
        End If
        Set MySh = .CreateObject("WScript.Shell")
        Set Fs = .CreateObject("Scripting.FileSystemObject") 'ファイル制御 
    End With


'***** ↓↓↓ConstructDecralations↓↓↓ *********************************************** 
    Const vbWq = """" ' 文字列データに ダブルクォーテーションを利用する為の変数。 
    'Const defType = 65 ' Popupメッセージの既定表示指定  


'***** ↓↓↓AuthDecralations↓↓↓ **************************************************** 


'***** ↓↓↓ValiableConstDecralations↓↓↓ ******************************************* 
    Public Pc ', Dummy 

    'Public vbWCrLf 
    'vbWCrLf = vbCrLf & vbCrLf 

    'Dim vbSep, vbWSep 
    'vbSep = "$" & "$" & "$" 
    'vbWsep = vbSep & vbSep 

    'Public sTitle, sRoot 
    'sTitle = Fs.GetBaseName(My.ScriptName) '実行スクリプト名取得  
    'sRoot = Fs.GetParentFolderName(My.ScriptName) '実行スクリプトフォルダ名取得  


'***** ↓↓↓PublicDecralations↓↓↓ ************************************************** 
    Public objProcS, objProc ' for pTerminate  


'***** ↓↓↓FileControlDecralations↓↓↓ ********************************************* 
    'Public win, System, UserProf, Roamings Explorer 

    'win = MySh.ExpandEnvironmentStrings("%systemroot%") 
    'System = Fs.BuildPath(win, "System32") 
    'UserProf = MySh.ExpandEnvironmentStrings("%UserProfile%") 
    'Roamings = MySh.ExpandEnvironmentStrings("%AppData%") 
    'Explorer = Fs.BuildPath(win,"Explorer.exe") & " /e," 


'***** ↓↓↓Decralations↓↓↓ ******************************************************** 
    Dim iTarget


'***** ↓↓↓MainRoutine↓↓↓ ********************************************************* 
    On Error Resume Next

    For Each iTarget in Parameters
        If IsNumeric(iTarget) Then ' 対象が数値なら待機時間扱い。 
            If Parameters.Count > 1 Then
                My.Sleep Clng(iTarget)
                Pc = 0
            Else
                My.Quit
            End If
        Else
            If Left(iTarget, 1) = "+" Then ' 先頭文字が + なら実行対象扱い。 
                iTarget = Replace(iTarget, "+", "")
                pExecute iTarget,,,,False ' プログラム実行開始処理。 
            Else
                pTerminate iTarget ' プロセス終了処理。 
            End If
        End If
    Next

    Set MySh = Nothing
    Set Fs = Nothing
    Set My = Nothing


'***** ↓↓↓ LimitedFunctions ↓↓↓ ************************************************** 


'***** ↓↓↓ GlobalFunctions ↓↓↓ *************************************************** 

    '------------------------------------ 
    ' chkParam - checkParameter  
    '    実行された関数のパラメーターが 未指定(エラー)や  
    '    空白でないかを確認し、Boolean値で返す。 
    ' - Created by LazwardFox -  

        ' Release 20150426 223532 

    Function chkParam(tgtParam)
        Dim Result
        Result = False
        If TypeName(tgtParam) = "Error" Then
        ElseIf IsEmpty(tgtParam) or tgtParam = "" Then
        Else
            Result = True
        End If
        chkParam = Result
    End Function


    '------------------------------------ 
    ' pExecute - ProcessExecute  
    ' - Created by LazwardFox -  

        ' with Fs, chkParam  

        ' Release 20150426 223532 
        ' βRelease 20150426 214438  

        ' pExecute(  
        '        strExe - 実行ファイルのフルパス名。 
        '        ,strPram - 未指定 ないし プログラムへのパラメータ文字列。 
        '        ,strExType - 実行対象のタイプ  
        '                        未指定 - [既定] 
        '                        DDE - ダイナミックリンクライブラリなどを利用する場合の指定。 
        '                        PNWQ - 実行プログラム指定でパラメータと分離しない必要がある場合のみ使用。 
        '        ,intType - Windowタイプ -  
        '                    0 ないし 未指定 - ウインドウ非表示(プロセス隠れます 非推奨)  
        '                    1 - 通常のウィンドウ表示(既定[推奨])  
        '        ,Wait - 数値(mSec) / True(実行したプログラムの終了を待つ) / False(待機せず次の処理へ[既定])  
        '     )  

    Function pExecute(strExe, strPram, strExType, intType, Wait)
        Dim sWq, pWq, rName, rTarget, wType, eWait, sSec
        If Fs.FileExists(strExe) Then

            sWq = vbWq
            pWq = vbWq
            If chkParam(strExType) Then
                If Ucase(CStr(strExType)) = "DDE" Then
                    sWq = ""
                    pWq = ""
                ElseIf Ucase(CStr(strExType)) = "PNWQ" Then
                    pWq = ""
                End If
            End If

            rName = vbWq & Fs.GetBaseName(strExe) & vbWq
            rTarget = sWq & CStr(strExe) & sWq
            If chkParam(strPram) Then
                rTarget = rTarget & " " & pWq & Cstr(strPram) & pWq
            End If

            If chkParam(intType) Then
                If IsNumeric(intType) Then
                    wType = Cint(intType)
                Else
                    wType = 0
                End If
            End If

            eWait = False
            sSec = 0
            If chkParam(Wait) Then
                If IsNumeric(Wait) Then
                    sSec = Clng(Wait)
                Else
                    eWait = Wait
                End If
            End If

            MySh.Run rTarget, wType, eWait
            My.Sleep sSec
        End If
    End Function


    '------------------------------------ 
    ' pTerminate - ProcessTerminate  
    ' - Created by LazwardFox -  

        ' Update 20150426 220229 - 前方一致検索のみ拡張対応。 
        ' Release 20150426 214438  
        
        ' pTerminate( 
        '      strProc - プロセス名を 大文字小文字完全一致で。 
        '     ) 

    Function pTerminate(strProc)
        'Dim objProcS, objProc ' 関数実行都度宣言するよりPublic宣言で・・・ 
        Dim intProc, nProc
        'If InStr(strProc, "*") Then 
        If Right(strProc, 1) = "*" Then
            strProc = Replace(strProc, "*", "")
        End If
        intProc = Len(strProc)
        If intProc <= 0 Then
        Else
            Set objProcS = GetObject("winmgmts:").InstancesOf("win32_process")
            For Each objProc In objProcS
                nProc = CStr(objProc.Name)
                nProc = Left(nProc, intProc)
                If nProc = strProc Then
                    objProc.Terminate
                    My.Sleep 200 ' 最低限の処理待ちを配置。 
                End If
            Next
            Set objProcS = Nothing
        End If
    End Function
◆ コッチも別途・・・
自己解凍zipで おきつね仕様のインストーラを・・・
tProcessInstaller.exe 20150427版
注) 前出のモノ同様、インストールしてデスクトップに出来るアイコンは、あくまでテンプレートで そのままでは機能しません。 そのショートカットをコピーし、[プロパティ]画面 [ショートカット]タブ [リンク先]枠に 任意で、 対象プロセス名 や 実行したいプログラムファイルのフルパス名、待機時間などを 追記して 利用して下さい。

・・・おきつねさまの運用の都合もあり、ココまで組んでは診たのだが。 コマンドラインパラメータの長さ(文字数)制限は、実行開始指定に用いるとなると これがかなり深刻で、 精々2つのプログラムを枠に記述出来るかドウかって域・・・ 多数のアプリケーションの終了や開始を 細かくスケジュールしたいとなると、iniファイルを用いるタイプでないと対応は難しいだろう・・・ と云うか、アプリケーションの起動をiniファイルでスケジュールするだけのモノなら、過去構成した物が既にあり、 (実際今回 Function pExecute に 関しては、ソチラのMainRoutineから処理を複製し、関数化した) コレは普段から利用しているので、ソチラに "プロセスの終了処理" を追加したほうが、恐らくは早いだろうな・・・(´ヘ`;)

・・・再度機能追加。 ■ 概要 ■
・ファイルの実行指定時に必要だった + 文字を、フルパス指定の
 実行ファイル名文字列で指定したファイルが存在している場合のみ不要に。
■ スクリプト本体 ■
'***** ↓↓↓ScriptTitle↓↓↓ ********************************************************* 
    ' tProcess.vbs - ProcessTerminator  
    ' - Created by LazwardFox -  

    ' Update  
    ' Update 20150501 045650  
    '        - フルパス指定の実行ファイル名文字列の場合は 
    '           + ナシでも ファイルが存在していれば処理するよう変更。 
    ' Update 20150427 072818 - 端折っていた分岐を加筆。 
    ' Update 20150426 235042 - 指定処理毎の待機時間指定可能に。 
    ' Update 20150426 224446  
    '         - 先頭一致検索でのプロセス停止と、プログラム実行開始指定を可能に。 
    ' Release 20150423 035751  
    ' Update 20150423 033144 - 関数名間違い/変数宣言忘れ/記述ミスを訂正。 
    ' Update 20150423 025152 - 処理開始までの待機時間指定を追加。 
    ' αRelease 20150423 023704  
    ' DevStart 20150423 021040  


'***** ↓↓↓ Memo ↓↓↓ ************************************************************** 

    '  


'***** ↓↓↓ ObjectDecralations ↓↓↓ ************************************************ 
    Option Explicit

    Dim My, Parameters, MySh, Fs
    Set My = WScript
    With My
        Set Parameters = .Arguments 'パラメーター取得 
        If Parameters.Count <= 0 Then 'パラメータなし起動の無効化 誤操作による起動を防止 
            .Quit
        End If
        Set MySh = .CreateObject("WScript.Shell")
        Set Fs = .CreateObject("Scripting.FileSystemObject") 'ファイル制御 
    End With


'***** ↓↓↓ConstructDecralations↓↓↓ *********************************************** 
    Const vbWq = """" ' 文字列データに ダブルクォーテーションを利用する為の変数。 
    'Const defType = 65 ' Popupメッセージの既定表示指定  


'***** ↓↓↓AuthDecralations↓↓↓ **************************************************** 


'***** ↓↓↓ValiableConstDecralations↓↓↓ ******************************************* 
    Public Pc ', Dummy 

    'Public vbWCrLf 
    'vbWCrLf = vbCrLf & vbCrLf 

    'Dim vbSep, vbWSep 
    'vbSep = "$" & "$" & "$" 
    'vbWsep = vbSep & vbSep 

    'Public sTitle, sRoot 
    'sTitle = Fs.GetBaseName(My.ScriptName) '実行スクリプト名取得  
    'sRoot = Fs.GetParentFolderName(My.ScriptName) '実行スクリプトフォルダ名取得  


'***** ↓↓↓PublicDecralations↓↓↓ ************************************************** 
    Public objProcS, objProc ' for pTerminate  


'***** ↓↓↓FileControlDecralations↓↓↓ ********************************************* 
    'Public win, System, UserProf, Roamings, Explorer 

    'win = MySh.ExpandEnvironmentStrings("%systemroot%") 
    'System = Fs.BuildPath(win, "System32") 
    'UserProf = MySh.ExpandEnvironmentStrings("%UserProfile%") 
    'Roamings = MySh.ExpandEnvironmentStrings("%AppData%") 
    'Explorer = Fs.BuildPath(win,"Explorer.exe") & " /e," 


'***** ↓↓↓Decralations↓↓↓ ******************************************************** 
    Dim iTarget


'***** ↓↓↓MainRoutine↓↓↓ ********************************************************* 
    On Error Resume Next

    For Each iTarget in Parameters
        If IsNumeric(iTarget) Then ' 対象が数値なら待機時間扱い。 
            If Parameters.Count > 1 Then
                My.Sleep Clng(iTarget)
                Pc = 0
            Else
                My.Quit
            End If
        ElseIf Fs.FileExists(iTarget) Then
            pExecute iTarget,,,,False ' プログラム実行開始処理。 
        Else
            If Left(iTarget, 1) = "+" Then ' 先頭文字が + なら実行対象扱い。 
                iTarget = Replace(iTarget, "+", "")
                pExecute iTarget,,,,False ' プログラム実行開始処理。 
            Else
                pTerminate iTarget ' プロセス終了処理。 
            End If
        End If
    Next

    Set MySh = Nothing
    Set Fs = Nothing
    Set My = Nothing


'***** ↓↓↓ LimitedFunctions ↓↓↓ ************************************************** 


'***** ↓↓↓ GlobalFunctions ↓↓↓ *************************************************** 

    '------------------------------------ 
    ' chkParam - checkParameter  
    '    実行された関数のパラメーターが 未指定(エラー)や  
    '    空白でないかを確認し、Boolean値で返す。 
    ' - Created by LazwardFox -  

        ' Release 20150426 223532 

    Function chkParam(tgtParam)
        Dim Result
        Result = False
        If TypeName(tgtParam) = "Error" Then
        ElseIf IsEmpty(tgtParam) or tgtParam = "" Then
        Else
            Result = True
        End If
        chkParam = Result
    End Function


    '------------------------------------ 
    ' pExecute - ProcessExecute  
    ' - Created by LazwardFox -  

        ' with Fs, chkParam  

        ' Release 20150426 223532 
        ' βRelease 20150426 214438  

        ' pExecute(  
        '        strExe - 実行ファイルのフルパス名。 
        '        ,strPram - 未指定 ないし プログラムへのパラメータ文字列。 
        '        ,strExType - 実行対象のタイプ  
        '                        未指定 - [既定] 
        '                        DDE - ダイナミックリンクライブラリなどを利用する場合の指定。 
        '                        PNWQ - 実行プログラム指定でパラメータと分離しない必要がある場合のみ使用。 
        '        ,intType - Windowタイプ -  
        '                    0 ないし 未指定 - ウインドウ非表示(プロセス隠れます 非推奨)  
        '                    1 - 通常のウィンドウ表示(既定[推奨])  
        '        ,Wait - 数値(mSec) / True(実行したプログラムの終了を待つ)  
        '                            / False(待機せず次の処理へ[既定])  
        '     )  

    Function pExecute(strExe, strPram, strExType, intType, Wait)
        Dim sWq, pWq, rName, rTarget, wType, eWait, sSec
        If Fs.FileExists(strExe) Then
            sWq = vbWq
            pWq = vbWq
            If chkParam(strExType) Then
                If Ucase(CStr(strExType)) = "DDE" Then
                    sWq = ""
                    pWq = ""
                ElseIf Ucase(CStr(strExType)) = "PNWQ" Then
                    pWq = ""
                End If
            End If

            rName = vbWq & Fs.GetBaseName(strExe) & vbWq
            rTarget = sWq & CStr(strExe) & sWq
            If chkParam(strPram) Then
                rTarget = rTarget & " " & pWq & Cstr(strPram) & pWq
            End If

            If chkParam(intType) Then
                If IsNumeric(intType) Then
                    wType = Cint(intType)
                Else
                    wType = 0
                End If
            End If

            eWait = False
            sSec = 0
            If chkParam(Wait) Then
                If IsNumeric(Wait) Then
                    sSec = Clng(Wait)
                Else
                    eWait = Wait
                End If
            End If

            MySh.Run rTarget, wType, eWait
            My.Sleep sSec
        End If
    End Function


    '------------------------------------ 
    ' pTerminate - ProcessTerminate  
    ' - Created by LazwardFox -  

        ' Update 20150426 220229 - 前方一致検索のみ拡張対応。 
        ' Release 20150426 214438  
        
        ' pTerminate( 
        '      strProc - プロセス名を 大文字小文字完全一致で。 
        '     ) 

    Function pTerminate(strProc)
        'Dim objProcS, objProc ' 関数実行都度宣言するよりPublic宣言で・・・ 
        Dim intProc, nProc
        'If InStr(strProc, "*") Then 
        If Right(strProc, 1) = "*" Then
            strProc = Replace(strProc, "*", "")
        End If
        intProc = Len(strProc)
        If intProc <= 0 Then
        Else
            Set objProcS = GetObject("winmgmts:").InstancesOf("win32_process")
            For Each objProc In objProcS
                nProc = CStr(objProc.Name)
                nProc = Left(nProc, intProc)
                If nProc = strProc Then
                    objProc.Terminate
                    My.Sleep 200 ' 最低限の処理待ちを配置。 
                End If
            Next
            Set objProcS = Nothing
        End If
    End Function
・・・の様にした上で、 ・・・更に調整。
'***** ↓↓↓ScriptTitle↓↓↓ ********************************************************* 
    ' tProcess.vbs - ProcessTerminator  
    ' - Created by LazwardFox -  

    ' Update  
    ' Update 20150501 053111 - pExecuteへ 指定に対する結果を返すよう追記し、 
    '                            メインルーチンをコンパクトに。 
    ' Update 20150501 045650 
    '        - フルパス指定の実行ファイル名文字列の場合は  
    '           + ナシでも ファイルが存在していれば処理するよう変更。  
    ' Update 20150427 072818 - 端折っていた分岐を加筆。 
    ' Update 20150426 235042 - 指定処理毎の待機時間指定可能に。 
    ' Update 20150426 224446 
    '         - 先頭一致検索でのプロセス停止と、プログラム実行開始指定を可能に。 
    ' Release 20150423 035751 
    ' Update 20150423 033144 - 関数名間違い/変数宣言忘れ/記述ミスを訂正。 
    ' Update 20150423 025152 - 処理開始までの待機時間指定を追加。 
    ' αRelease 20150423 023704  
    ' DevStart 20150423 021040  


'***** ↓↓↓ Memo ↓↓↓ ************************************************************** 

    '  


'***** ↓↓↓ ObjectDecralations ↓↓↓ ************************************************ 
    Option Explicit

    Dim My, Parameters, MySh, Fs
    Set My = WScript
    With My
        Set Parameters = .Arguments 'パラメーター取得 
        If Parameters.Count <= 0 Then 'パラメータなし起動の無効化 誤操作による起動を防止 
            .Quit
        End If
        Set MySh = .CreateObject("WScript.Shell")
        Set Fs = .CreateObject("Scripting.FileSystemObject") 'ファイル制御 
    End With


'***** ↓↓↓ConstructDecralations↓↓↓ *********************************************** 
    Const vbWq = """" ' 文字列データに ダブルクォーテーションを利用する為の変数。 
    'Const defType = 65 ' Popupメッセージの既定表示指定  


'***** ↓↓↓AuthDecralations↓↓↓ **************************************************** 


'***** ↓↓↓ValiableConstDecralations↓↓↓ ******************************************* 
    Public Pc ', Dummy 

    'Public vbWCrLf 
    'vbWCrLf = vbCrLf & vbCrLf 

    'Dim vbSep, vbWSep 
    'vbSep = "$" & "$" & "$" 
    'vbWsep = vbSep & vbSep 

    'Public sTitle, sRoot 
    'sTitle = Fs.GetBaseName(My.ScriptName) '実行スクリプト名取得  
    'sRoot = Fs.GetParentFolderName(My.ScriptName) '実行スクリプトフォルダ名取得  


'***** ↓↓↓PublicDecralations↓↓↓ ************************************************** 
    Public objProcS, objProc ' for pTerminate  


'***** ↓↓↓FileControlDecralations↓↓↓ ********************************************* 
    'Public win, System, UserProf, Roamings, Explorer 

    'win = MySh.ExpandEnvironmentStrings("%systemroot%") 
    'System = Fs.BuildPath(win, "System32") 
    'UserProf = MySh.ExpandEnvironmentStrings("%UserProfile%") 
    'Roamings = MySh.ExpandEnvironmentStrings("%AppData%") 
    'Explorer = Fs.BuildPath(win,"Explorer.exe") & " /e," 


'***** ↓↓↓Decralations↓↓↓ ******************************************************** 
    Dim iTarget


'***** ↓↓↓MainRoutine↓↓↓ ********************************************************* 
    On Error Resume Next

    For Each iTarget in Parameters
        If IsNumeric(iTarget) Then ' 対象が数値なら待機時間扱い。 
            If Parameters.Count > 1 Then
                My.Sleep Clng(iTarget)
                Pc = 0
            Else
                My.Quit
            End If
        ElseIf Left(iTarget, 1) = "+" Then ' 先頭文字が + なら実行対象扱い。 
            iTarget = Replace(iTarget, "+", "")
            pExecute iTarget,,,,False ' プログラム実行開始処理。 
        Else
            If pExecute(iTarget,,,,False) Then
            Else
                pTerminate iTarget ' プロセス終了処理。 
            End If
        End If
    Next

    Set MySh = Nothing
    Set Fs = Nothing
    Set My = Nothing


'***** ↓↓↓ LimitedFunctions ↓↓↓ ************************************************** 


'***** ↓↓↓ GlobalFunctions ↓↓↓ *************************************************** 

    '------------------------------------ 
    ' chkParam - checkParameter  
    '    実行された関数のパラメーターが 未指定(エラー)や  
    '    空白でないかを確認し、Boolean値で返す。 
    ' - Created by LazwardFox -  

        ' Release 20150426 223532 

    Function chkParam(tgtParam)
        Dim Result
        Result = False
        If TypeName(tgtParam) = "Error" Then
        ElseIf IsEmpty(tgtParam) or tgtParam = "" Then
        Else
            Result = True
        End If
        chkParam = Result
    End Function


    '------------------------------------ 
    ' pExecute - ProcessExecute  
    ' - Created by LazwardFox -  

        ' with Fs, chkParam  

        ' Update 20150501 052456 
        '      - 指定文字鉄がファイル名でなかったらFalseを返します。 
        ' Release 20150426 223532 
        ' βRelease 20150426 214438  

        ' pExecute(  
        '        strExe - 実行ファイルのフルパス名。 
        '        ,strPram - 未指定 ないし プログラムへのパラメータ文字列。 
        '        ,strExType - 実行対象のタイプ  
        '                        未指定 - [既定] 
        '                        DDE - ダイナミックリンクライブラリなどを利用する場合の指定。 
        '                        PNWQ - 実行プログラム指定でパラメータと分離しない必要がある場合のみ使用。 
        '        ,intType - Windowタイプ -  
        '                    0 ないし 未指定 - ウインドウ非表示(プロセス隠れます 非推奨)  
        '                    1 - 通常のウィンドウ表示(既定[推奨])  
        '        ,Wait - 数値(mSec) / True(実行したプログラムの終了を待つ)  
        '                            / False(待機せず次の処理へ[既定])  
        '     )  

    Function pExecute(strExe, strPram, strExType, intType, Wait)
        Dim sWq, pWq, rName, rTarget, wType, eWait, sSec
        If Fs.FileExists(strExe) Then
            sWq = vbWq
            pWq = vbWq
            If chkParam(strExType) Then
                If Ucase(CStr(strExType)) = "DDE" Then
                    sWq = ""
                    pWq = ""
                ElseIf Ucase(CStr(strExType)) = "PNWQ" Then
                    pWq = ""
                End If
            End If

            rName = vbWq & Fs.GetBaseName(strExe) & vbWq
            rTarget = sWq & CStr(strExe) & sWq
            If chkParam(strPram) Then
                rTarget = rTarget & " " & pWq & Cstr(strPram) & pWq
            End If

            If chkParam(intType) Then
                If IsNumeric(intType) Then
                    wType = Cint(intType)
                Else
                    wType = 0
                End If
            End If

            eWait = False
            sSec = 0
            If chkParam(Wait) Then
                If IsNumeric(Wait) Then
                    sSec = Clng(Wait)
                Else
                    eWait = Wait
                End If
            End If

            MySh.Run rTarget, wType, eWait
            My.Sleep sSec
            pExecute = True
        Else
            pExecute = False
        End If
    End Function


    '------------------------------------ 
    ' pTerminate - ProcessTerminate  
    ' - Created by LazwardFox -  

        ' Update 20150426 220229 - 前方一致検索のみ拡張対応。 
        ' Release 20150426 214438  
        
        ' pTerminate( 
        '      strProc - プロセス名を 大文字小文字完全一致で。 
        '     ) 

    Function pTerminate(strProc)
        'Dim objProcS, objProc ' 関数実行都度宣言するよりPublic宣言で・・・ 
        Dim intProc, nProc
        'If InStr(strProc, "*") Then 
        If Right(strProc, 1) = "*" Then
            strProc = Replace(strProc, "*", "")
        End If
        intProc = Len(strProc)
        If intProc <= 0 Then
        Else
            Set objProcS = GetObject("winmgmts:").InstancesOf("win32_process")
            For Each objProc In objProcS
                nProc = CStr(objProc.Name)
                nProc = Left(nProc, intProc)
                If nProc = strProc Then
                    objProc.Terminate
                    My.Sleep 200 ' 最低限の処理待ちを配置。 
                End If
            Next
            Set objProcS = Nothing
        End If
    End Function

・・・可能な範囲で仕上げたコトに。 ■ 概要 ■
・pExecute を 改良し、スクリプトとして MySh.Run の 全機能を使える構造に変更。


■ 最終的には この様に [リンク先]に記述して利用するカタチになってます。
▼ 待機時間指定ナシで、BlueStacksを終了させ、msi AfterburnerRemoteServerを開始する例。
"C:\Program Files\Okitsunesama\Scripts\ProcessTerminator\tProcess.vbe" HD-* "C:\Program Files (x86)\MSI Afterburner\RemoteServer\MSIAfterburnerRemoteServer.exe"
・・・コマンドプロンプトでの利用でも 同様の書式になります。 ▼ 開始待機時間1秒の後、BlueStacksを終了させつつ 6秒待機し、
"C:\Program Files\Okitsunesama\Scripts\ProcessTerminator\tProcess.vbe" 1000 HD-* 6000 "C:\Program Files (x86)\MSI Afterburner\RemoteServer\MSIAfterburnerRemoteServer.exe,,,,5000" 4000
・・・ + 指定を使わずに msi AfterburnerRemoteServerを開始しつつ 5秒待機、その後 更に4秒待機する例。
また、起動するアプリケーションによっては、カレントディレクトリとしてのみ稼動するモノがあり、 そう云うタイプのモノは、VBScriptとショートカットの仕様の関係で、1つしか指定出来ません。 その対処方法とは・・・ ショートカットのプロパティ [ショートカット]タブで、
 [作業フォルダ]に、そうしたプログラムの
 導入されているフォルダを指定。
PSO2のランチャを起動し、[ゲーム開始]された 3min20sec後に PSO2ヨミ を 起動する [リンク先]の記述例。
"C:\Program Files\Okitsunesama\Scripts\ProcessTerminator\tProcess.vbe" "C:\Program Files (x86)\SEGA\PHANTASYSTARONLINE2\pso2_bin\pso2launcher.exe,,,,True" 200000 "C:\Program Files (Free)\StreamingSet\PSO2yomi\PSO2yomi.exe,,,,False"
OP再生してから・・・ とかだと、300000(5min)くらいがイイみたい。
■ スクリプト本体 ■
'***** ↓↓↓ScriptTitle↓↓↓ ********************************************************* 
    ' tProcess.vbs - ProcessTerminator  
    ' - Created by LazwardFox -  

    ' Update  
    ' Update 20150501 070815 - pExecute を 基スクリプトのものと同じ形式に戻して、 
                            '    プログラムへのパラーメーター受け渡しと、 
                            '    アプリケーションの待機指定を可能に。 
    ' Update 20150501 053111 - pExecute へ 指定に対する結果を返すよう追記し、 
                            '    メインルーチンをコンパクトに。 
    ' Update 20150501 045650 - フルパス指定の実行ファイル名文字列の場合は  
                            '    + ナシでも ファイルが存在していれば処理するよう変更。  
    ' Update 20150427 072818 - 端折っていた分岐を加筆。 
    ' Update 20150426 235042 - 指定処理毎の待機時間指定可能に。 
    ' Update 20150426 224446 - 先頭一致検索でのプロセス停止と、 
                            '    プログラム実行開始指定を可能に。 
    ' Release 20150423 035751 
    ' Update 20150423 033144 - 関数名間違い/変数宣言忘れ/記述ミスを訂正。 
    ' Update 20150423 025152 - 処理開始までの待機時間指定を追加。 
    ' αRelease 20150423 023704  
    ' DevStart 20150423 021040  


'***** ↓↓↓ Memo ↓↓↓ ************************************************************** 

    '  


'***** ↓↓↓ ObjectDecralations ↓↓↓ ************************************************ 
    Option Explicit

    Dim My, Parameters, MySh, Fs
    Set My = WScript
    With My
        Set Parameters = .Arguments 'パラメーター取得 
        If Parameters.Count <= 0 Then 'パラメータなし起動の無効化 誤操作による起動を防止 
            .Quit
        End If
        Set MySh = .CreateObject("WScript.Shell")
        Set Fs = .CreateObject("Scripting.FileSystemObject") 'ファイル制御 
    End With


'***** ↓↓↓ConstructDecralations↓↓↓ *********************************************** 
    Const vbWq = """" ' 文字列データに ダブルクォーテーションを利用する為の変数。 
    'Const defType = 65 ' Popupメッセージの既定表示指定  


'***** ↓↓↓AuthDecralations↓↓↓ **************************************************** 


'***** ↓↓↓ValiableConstDecralations↓↓↓ ******************************************* 
    Public Pc ', Dummy 

    'Public vbWCrLf 
    'vbWCrLf = vbCrLf & vbCrLf 

    'Dim vbSep, vbWSep 
    'vbSep = "$" & "$" & "$" 
    'vbWsep = vbSep & vbSep 

    'Public sTitle, sRoot 
    'sTitle = Fs.GetBaseName(My.ScriptName) '実行スクリプト名取得  
    'sRoot = Fs.GetParentFolderName(My.ScriptName) '実行スクリプトフォルダ名取得  


'***** ↓↓↓PublicDecralations↓↓↓ ************************************************** 
    Public objProcS, objProc ' for pTerminate  


'***** ↓↓↓FileControlDecralations↓↓↓ ********************************************* 
    'Public win, System, UserProf, Roamings, Explorer 

    'win = MySh.ExpandEnvironmentStrings("%systemroot%") 
    'System = Fs.BuildPath(win, "System32") 
    'UserProf = MySh.ExpandEnvironmentStrings("%UserProfile%") 
    'Roamings = MySh.ExpandEnvironmentStrings("%AppData%") 
    'Explorer = Fs.BuildPath(win,"Explorer.exe") & " /e," 


'***** ↓↓↓Decralations↓↓↓ ******************************************************** 
    Dim iTarget, arTargets


'***** ↓↓↓MainRoutine↓↓↓ ********************************************************* 
    On Error Resume Next

    For Each iTarget in Parameters
        arTargets = Split(iTarget, ",")
        If IsNumeric(arTargets(0)) Then ' 対象が数値なら待機時間扱い。 
            If Parameters.Count > 1 Then
                My.Sleep Clng(arTargets(0))
                Pc = 0
            Else
                My.Quit
            End If
        ElseIf Left(arTargets(0), 1) = "+" Then ' 先頭文字が + なら実行対象扱い。 
            arTargets(0) = Replace(arTargets(0), "+", "")
            pExecute arTargets ' プログラム実行開始処理。 
        Else
            If pExecute(arTargets) Then
            Else
                pTerminate arTargets(0) ' プロセス終了処理。 
            End If
        End If
    Next

    Set MySh = Nothing
    Set Fs = Nothing
    Set My = Nothing


'***** ↓↓↓ LimitedFunctions ↓↓↓ ************************************************** 


'***** ↓↓↓ GlobalFunctions ↓↓↓ *************************************************** 

    '------------------------------------ 
    ' chkParam - checkParameter  
    ' - Created by LazwardFox -  

        ' 実行された関数のパラメーターが 未指定(エラー)や  
        ' 空白でないかを確認し、Boolean値で返す。 

        ' Update 20150507 180331 
        ' Release 20150426 223532 

    Function chkParam(tgtParam)
        Dim Result
        Result = False
        If VarType(tgtParam) = 10 Then
        ElseIf IsEmpty(tgtParam) or tgtParam = "" Then
        Else
            Result = True
        End If
        chkParam = Result
    End Function


    '------------------------------------ 
    ' pExecute - ProcessExecute  
    ' - Created by LazwardFox -  

        ' with Fs, chkParam  

        ' Update 20150501 070815 - 基の形式に戻し、且つ 一部変数解析を改良。 
                                '    指定先頭配列が 既存ファイル名でなかったら 
                                '    処理を終了し、False を 返します。 
        ' Update 20150501 052456 - 指定文字列が ファイル名でなかったら 
                                '    False を 返します。 
        ' Release 20150426 223532 
        ' βRelease 20150426 214438  

        ' blResult = pExecute(arTargets)  

            ' blResult - 実行結果 Boolean値を返す。 
            ' arTargets(0) - strExe - 実行ファイルのフルパス名。 
            ' arTargets(1) - strPram - プログラムへのパラメータ文字列。 
            ' arTargets(2) - strExType - 実行対象のタイプ  
                ' 未指定 - [既定] 
                ' DDE - ダイナミックリンクライブラリなどを利用する場合の指定。 
                ' PNWQ - 実行プログラム指定でパラメータと 
                        ' 分離しない必要がある場合のみ使用。 
            ' arTargets(3) - intType - Windowタイプ。 
                ' Microsoft VBScriptリファレンス - Runメソッド IntWindowStyle
                ' https://msdn.microsoft.com/ja-jp/library/cc364421.aspx
                ' 0 - ウインドウ非表示(プロセス隠れます 非推奨)  
                ' 10 ないし 未指定 - 通常のウィンドウ表示 [既定](推奨) 
            ' arTargets(4) - Wait - 待機指定。 
                    ' 数値 - mSec指定 
                    ' True - 実行したプログラムの終了を待つ、利用要注意。 
                    ' False ないし 未指定  - 待機せず次の処理へ [既定] 


    'Function pExecute(strExe, strPram, strExType, intType, Wait) 
    Function pExecute(arTargets)
        On Error Resume Next
        Dim intUBound, chkTgt4
        Dim sWq, pWq, rName, rTarget, wType, eWait, sSec
        If Fs.FileExists(arTargets(0)) Then
            intUBound = UBound(arTargets)
            
            ' 実行ファイル 及び パラメーター文字列 を  
            ' ダブルクォーテーションで囲むか否かを処理。 
            sWq = vbWq
            pWq = vbWq
            If intUBound >= 2 Then 
                If chkParam(arTargets(2)) Then
                    If Ucase(CStr(arTargets(2))) = "DDE" Then
                        sWq = ""
                        pWq = ""
                    ElseIf Ucase(CStr(arTargets(2))) = "PNWQ" Then
                        pWq = ""
                    End If
                End If
            End If

            ' MySh.Run への 第1引数(実行ファイル名)を整える。 
            rName = vbWq & Fs.GetBaseName(arTargets(0)) & vbWq
            rTarget = sWq & CStr(arTargets(0)) & sWq

            ' MySh.Run への 第1引数(実行ファイルに対するパラメーター文字列)を整える。 
            If intUBound >= 1 Then
                If chkParam(arTargets(1)) Then
                    rTarget = rTarget & " " & pWq & Cstr(arTargets(1)) & pWq
                End If
            End If

            ' MySh.Run への 第2引数(ウィンドウタイプ)を整える。 
            wType = 10
            If intUBound >= 3 Then
                If chkParam(arTargets(3)) Then
                    If IsNumeric(arTargets(3)) Then
                        wType = arTargets(3)
                    End If
                End If
            End If

            ' MySh.Run への 第3引数(待機指定)を整える。 
            eWait = False
            If intUBound >= 4 Then
                If chkParam(arTargets(4)) Then
                    chkTgt4 = Clng(arTargets(4))
                    If VarType(chkTgt4) = 3 Then
                        sSec = arTargets(4)
                    Else
                        chkTgt4 = CBool(arTargets(4))
                        If VarType(chkTgt4) = 11 Then
                            eWait = arTargets(4)
                        End If
                    End If
                End If
            End If

            ' 指定された処理を実行。 
            MySh.Run rTarget, wType, eWait
            My.Sleep sSec
            pExecute = True
        Else
            pExecute = False
        End If
    End Function


    '------------------------------------ 
    ' pTerminate - ProcessTerminate  
    ' - Created by LazwardFox -  

        ' Update 20150426 220229 - 前方一致検索のみ拡張対応。 
        ' Release 20150426 214438  
        
        ' pTerminate( 
        '      strProc - プロセス名を 大文字小文字完全一致で。 
        '     ) 

    Function pTerminate(strProc)
        'Dim objProcS, objProc ' 関数実行都度宣言するよりPublic宣言で・・・ 
        Dim intProc, nProc
        'If InStr(strProc, "*") Then 
        If Right(strProc, 1) = "*" Then
            strProc = Replace(strProc, "*", "")
        End If
        intProc = Len(strProc)
        If intProc <= 0 Then
        Else
            Set objProcS = GetObject("winmgmts:").InstancesOf("win32_process")
            For Each objProc In objProcS
                nProc = CStr(objProc.Name)
                nProc = Left(nProc, intProc)
                If nProc = strProc Then
                    objProc.Terminate
                    My.Sleep 200 ' 最低限の処理待ちを配置。 
                End If
            Next
            Set objProcS = Nothing
        End If
    End Function
◆ 例に寄って・・・
自己解凍zipで おきつね仕様のインストーラを・・・
tProcessInstaller.exe 20150508版
注) 前出のモノ同様、インストールしてデスクトップに出来るアイコンは、あくまでテンプレートで そのままでは機能しません。 そのショートカットをコピーし、[プロパティ]画面 [ショートカット]タブ [リンク先]枠に 任意で、 対象プロセス名 や 実行したいプログラムファイルのフルパス名、待機時間などを 追記して 利用して下さい。

・・・常駐停止用途で このスクリプトを利用する場合についてメモ。 ■ 概要 ■
・SYSTEMとして開始されたサービスのうち 終了できない対象があったので検証
◆ この2つは・・・
HD-UpdaterService.exe
HD-LogRotatorService.exe
 サービスである為、現在の版の tProcessからは終了させる手段がありません。
もし これらの常駐を、Windowsの起動時にだけでも回避したいのなら、
[タスクマネージャ] の [サービス]タブ右下、[サービス]ボタンからも開ける [サービス]管理画面で、
BlueStacks Log Rotator Service
BlueStacks Updater Service
 の2つの
 [プロパティ]-[全般]タブ、[スタートアップの種類] で
[手動] ないし [無効](非推奨)
 の いずれかを選択し、
 設定しておくしかない模様。
ただ、この設定を行っても・・・
BlueStacks を [スタートメニュー] や [デスクトップのアイコン] から任意で起動すると、
Log Rotator Service
 は、起動しませんが、
Updater Service
 は、仮に[無効]にしてあっても動作を開始し、
 [スタートアップの種類] を [自動] に 戻してしまいます。
また、BlueStacks Android Service も Updater Service と 同様の挙動をします・・・
tProcess は、多くのプロセスを停止出来ます・・・ パラメータ指定を誤るとシステムプロセスも対象になる程に(^_^;) しかし 20150503 090217現在、サービスを停止させる処理は追記していません。 利用の際は それらの点を充分に踏まえた上で、自己責任にてお願いします。

0 件のコメント:

コメントを投稿

注: コメントを投稿できるのは、このブログのメンバーだけです。