Lancer une application en VbScript en la passant en paramètre
Lancer un programme via VbScript en indiquant son chemin en paramètres. L'application sera lancée pour une durée limitée.
Option Explicit
'Launch an application with a limited duration
Dim WSHShell, MonExe, PIDMonExe
Dim DateLancement
Dim CheminExeALancer, MaxDuration
Function ParseCommand()
'
' Parses the command line and fills the script variables
' with the appropriate values.
'
Dim ArgCount
Dim objArgs
Set objArgs = Wscript.Arguments
ArgCount = 0
if objArgs.Count = 0 then
wscript.echo "No arguments specified."
wscript.echo
call Help()
end if
While ArgCount < objArgs.Count
Select Case LCase(objArgs(ArgCount))
Case "-app"
ArgCount = ArgCount + 1
CheminExeALancer=LCase(objArgs(ArgCount))
wscript.echo "CheminExeALancer : " & CheminExeALancer
Case "-duration"
ArgCount = ArgCount + 1
MaxDuration=LCase(objArgs(ArgCount))
If IsNumeric(MaxDuration) = True Then
MaxDuration = CInt(MaxDuration)
wscript.echo "MaxDuration : " & MaxDuration
Else
wscript.echo "-duration : n'est pas un numérique " & MaxDuration
Wscript.Quit
End If
Case Else:
wscript.echo "Invalid command."
wscript.echo
call Help()
wscript.quit
End Select
ArgCount = ArgCount + 1
Wend
End Function
sub Help()
'
' Display command-line syntax for the script.
'
wscript.echo "Script Function details"
wscript.echo "Syntax:"
wscript.echo
wscript.echo "-app ""patch to the source exe"""
wscript.echo "-duration ""maximal time execution for the app"""
wscript.echo
wscript.echo "Example"
wscript.echo
wscript.echo "VbLaunchAppForALimitedDurationWithParameters.vbs -app ""Notepad.exe C:\un fichier.txt"" -duration 5"
wscript.quit
End Sub
'Parse the command line.
call ParseCommand()
Set WSHShell = CreateObject("WScript.Shell")
Set MonExe = WSHShell.Exec(CheminExeALancer) 'Lancement de Notepad
PIDMonExe = MonExe.ProcessID
DateLancement = Now
'Wscript.echo "PID de l'exe : " & PIDMonExe
'On attend que le programme soit terminé
Do While MonExe.Status = 0
WScript.Sleep 1000 'Attente de 1 seconde
Wscript.echo DateDiff("s", DateLancement, Now)
If DateDiff("s", DateLancement, Now) > MaxDuration Then
MonExe.Terminate()
Exit Do
End If
Loop
'Wscript.echo "Exe terminé"
Set MonExe = Nothing
Set WSHShell = Nothing
Lien vers le fichier : cliquez ici
Publicité
Article(s) suivant(s)
Article(s) précédent(s)
Article(s) en relation(s)