Escolar Documentos
Profissional Documentos
Cultura Documentos
2
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set oFS = CreateObject("Scripting.FileSystemObject")
strWarning = "Due to network maintenance, this computer must be restarted. You have 5
minutes to save your work from the start of this countdown. Sorry for any
inconvenience caused. "
strDelay = 300 'Delay given in seconds; change this value to your preference, or
set it to 0 to give no delay at all
'Open a text file of computer names
'with one computer name per line
Set oTS = oFS.OpenTextFile("C:\computers.txt")
'go through the text file
Do Until oTS.AtEndOfStream
'get the next computer name
'store it in variable strCompname
strCompname = oTS.ReadLine
WshShell.Run "C:\Windows\System32\shutdown.exe -m \\" & strCompname
& " -r -c " & Chr(34) & strWarning & Chr(34) & " -t " & strDelay 'Replace the "-r"
switch with "-s" to make the computers shutdown instead
Loop
'close the text file
oTS.Close
3
However, that is where the benefits stop. Anything more than 20 lines, batch scripting gets very
complicated and very difficult to debug. You need a real language. Here is a "complex" example I
grabbed from Wikipedia:
1 @echo off
2 color 3
3 title Conditional Shutdown
4 set /p name=enter a name:
5 :start
6 cls
7 echo Hi, %name%
8 echo.
9 echo 1.Shutdown
10 echo 2.Quit
11 set /p choice=enter your choice 1,2:
12 if %choice%==1 goto shutdown
13 if %choice%==2 exit
14
15 :shutdown
16 cls
17 set /p sec=enter the number of seconds that you wish the computer to shutdown in:
18 set /p msg=enter the shutdown message you wish to display:
19 shutdown -s -f -t %sec% -c "%msg%"
20 echo shutdown initiated at %time%
21 set /p cancel=type cancel to stop shutdown
22 if %cancel%==cancel shutdown -a
23 if %cancel%==cancel goto start
A simple shutdown script. Anything beyond this, I would start writing an AutoIt file. Here is my AutoIt
version following the same logic flow as the original.
1 #comments-start
2 This code is MIT licensed by Dan Nagle
3 #comments-end
4
5 #include
6
7 Opt('MustDeclareVars', 1)
8
9 Dim $name = InputBox("Conditional Shutdown", "enter a name:")
10
11 If $name == "" Then
12 Exit
13 EndIf
14
15 Dim $msg
16 Dim $guiHandle = GUICreate("Hi, " & $name, 200, 60)
17 Dim $blurbLabel = GUICtrlCreateLabel("enter your choice", 5, 5)
18 Dim $shutdownbutton = GUICtrlCreateButton("Shutdown", 5, 30, 75)
19 Dim $exitbutton = GUICtrlCreateButton("Exit", 120, 30, 75)
20
21 GUISetState(@SW_SHOW, $guiHandle )
22 While 1
23 $msg = GUIGetMsg($guiHandle )
24
25 Select
26 Case $msg == $GUI_EVENT_CLOSE Or $msg = $exitbutton
27 Exit
28
29 Case $msg == $shutdownbutton
30
31 Dim $seconds = InputBox("Number of Seconds", "Enter the number of seconds that you wish
32
33 If $seconds == "" Then
34 ContinueLoop
35 EndIf
36
37 Dim $shutdownMsg = InputBox("Shutdown message", "Enter the shutdown message you wish to
38 If $shutdownMsg == "" Then
39 ContinueLoop
40 EndIf
41
42 ;do shutdown
43 RunWait(@ComSpec & " /c " & "shutdown -s -f -t " & $seconds &" -c """&a
44
45 Case Default
46 EndSelect
47 WEnd
The AutoIt version is a little more verbose, but I find it easier to read. It is also much more powerful.
It is a real scripting language with scope, functions, etc. It can do some complicated things and keep
you organized. Plus, unlike batch, it has a GUI library.
4
This is my function (which may have been done better elsewhere). It returns the time in ticks
(milliseconds, approximately) since the last user input:
DllStructSetData($struct, 1, DllStructGetSize($struct));
DllCall("user32.dll", "int", "GetLastInputInfo", "ptr",
DllStructGetPtr($struct))
; Get current ticks since last restart
Local $aTicks = DllCall("Kernel32.dll", "int", "GetTickCount")
; Return time since last activity, in ticks (approx milliseconds)
Local $iDiff = $aTicks[0] - DllStructGetData($struct, 2)
If $iDiff > 0 Then
; Normal return
Return $iDiff
Else
; Rollover of ticks counter has occured
Return SetError(0, 1, $aTicks[0])
EndIf
EndFunc
;==>_Timer_GetIdleTime
5
this script will check if mouse have moved
#NoTrayIcon
$pos = MouseGetPos()
$X=$pos[0]
$Y=$pos[1]
msgbox(64,"",$X & ", " &
$Y)
$begin = TimerInit()
sleep(5000)
$dif = TimerDiff($begin)
$pos1 = MouseGetPos()
$X1=$pos1[0]
$Y1=$pos1[1]
if $X<>$X1 or $Y<>$Y1 Then
MsgBox(64,"moved","You have moved the mouse")
Else
MsgBox(64,"Not moved","No Clicked")
EndIf
AdLibEnable("_UpdateTimer", 100)
Global $nTimer = 0
Global $OldMousePosition = MouseGetPos()
GLobal $NewMousePosition
While(1)
sleep(10)
WEnd
Func _UpdateTimer()
$nTimer += 100 ;100 ms
$NewMousePosition = MouseGetPos()
if (($NewMousePosition[0] == $OldMousePosition[0]) and
($NewMousePosition[1] == $OldMousePosition[1])) Then
; The user is still idle (you might also want to check for key
presses
Else
; The user moved, we should start the timer over.
$OldMousePosition = $NewMousePosition
$nTimer = 0
EndIf
if ($nTimer >= 5000) Then ;Change the '5000' to the number in
milliseconds you want to wait, with your user being idle.
_OnTimerFinish()
$nTimer = 0
EndIf
EndFunc
Func _OnTimerFinish()
MsgBox(0, "", "[b]idle hands are the devil's tools[/b]")
AdLibDisable()
Exit
EndFunc