Au3腳本是AutoIt3 Windows自動安裝腳本語言。AutoIt 是一種自動控制工具。它可以被用來自動完成任何基于 Windows 或 DOS 的簡單任務(wù)。它最初被設(shè)計用來自動完成安裝那些其它方法不能自動安裝的軟件。
Q1 如何調(diào)試腳本?
MsgBox(0,"測試",$var)
ConsoleWrite("var=" & $var & @CRLF)
Q2 操作CMD相關(guān)命令
Q2.1 如何運行DOS命令?
Run(@ComSpec & ' /c dir>d:\dir.txt',"", @SW_HIDE)
#include <Process.au3>
$rc = _RunDos("start Http://www.autoitx.com")
Q2.2 運行DOS命令如何連接AU3變量?
Local $var="d:\dir.txt"
Run(@ComSpec & ' /c dir>"'&$var&'"',"", @SW_HIDE)
Q2.3 運行DOS命令如何自動應(yīng)答?(注意:這并不屬于AU3的問題,這里附帶說一下。)
RunWait(@ComSpec & ' /c echo y|cacls %systemroot%\system32\wpcap.dll /d everyone', @SystemDir, @SW_HIDE)
Q2.4 多層DOS命令如何用?如netsh,diskpart等。
$dns="192.168.0.1"
RunWait(@ComSpec & ' /C netsh -c interface ip set dns 本地連接 source=static addr="' & $dns &'" register=PRIMARY ',"", @SW_HIDE )
Q2.5 運行DOS命令如何直接截取回顯?
;注意:回顯截取只支持Run而不是RunWait
#include <Constants.au3>
Opt("MustDeclareVars",1)
_test()
Func _test()
Local $foo,$line,$lines
$foo = Run(@ComSpec & " /c sc query Alerter", @SystemDir, @SW_HIDE, $STDOUT_CHILD)
$lines = ""
While 1
$line = StdoutRead($foo)
If @error Then ExitLoop
$lines &= $line
Wend
MsgBox(0,"test",$lines)
EndFunc
Q3 如何防止程序重復(fù)運行?
$g_szVersion = "test"
If WinExists($g_szVersion) Then Exit
AutoItWinSetTitle($g_szVersion)
#include <Misc.au3>
_Singleton("test")
Q4 如何直接運行系統(tǒng)程序關(guān)聯(lián)的文件?如[.txt, .msi, .pdf, .jpg, .lnk, .msc]等等。!
ShellExecute("Notepad.exe")
ShellExecute("test.txt", "", @ScriptDir, "edit")
ShellExecute("http://www.autoitx.com")
ShellExecute("C:\boot.ini", "", "", "print")
ShellExecute("test.lnk","",@ScriptDir)
ShellExecute("gpedit.msc", "", "", "open", @SW_MAXIMIZE)
Q5 如何控制系統(tǒng)服務(wù)?
API的控制服務(wù)
_StartService() 開始服務(wù)
_StopService() 停止服務(wù)
_ServiceExists() 檢測服務(wù)
_ServiceRunning() 運行服務(wù)
_CreateService() 建立服務(wù)
_DeleteService() 刪除服務(wù)
WMI的控制服務(wù)
_ServStart() 開始服務(wù)
_ServStop() 停止服務(wù)
_ServDelete() 刪除服務(wù)
_ServGetDetails() 服務(wù)詳情
_ServGetState() 服務(wù)狀態(tài)
_ServListInstalled() 服務(wù)列表
_ServPause() 暫停服務(wù)
_ServResume() 服務(wù)改名
_SerSetState() 設(shè)置服務(wù)狀態(tài)
http://www.autoitx.com/viewthread.php?tid=871&extra=page%3D1
Q6 如何操作注冊表?
Q6.1 常用的注冊表設(shè)置
;讀取注冊表指定的值
$var = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion", "ProgramFilesDir")
MsgBox(4096, "Program files 文件夾位于:", $var)
;創(chuàng)建一個主鍵、子鍵或值項。
RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Test", "TestKey", "REG_SZ", "Hello this is a test")
;刪除注冊表指定的值 (注意:這里刪除的是鍵項,而不是鍵值。)
RegDelete("HKEY_LOCAL_MACHINE\SOFTWARE", "TestKey")
;其他還有RegEnumKey(),RegEnumVal(),詳細應(yīng)用請參考幫助。
Q6.2 注冊表權(quán)限設(shè)置
http://www.autoitx.com/viewthrea ... hlight=%C8%A8%CF%DE
Q7 如何不重啟刷新注冊表馬上生效?
Do
ProcessClose("explorer.exe")
Until Not ProcessExists("explorer.exe")
Run("gpupdate /force","",@SW_HIDE)
;強烈推存應(yīng)用這個
DllCall("user32.dll","int","SendMessageTimeout","hwnd",65535,"int",26,"int",0,"int",0,"int",0,"int",1000,"str","dwResult")
Q8 AU3編寫的程序如何帶參數(shù)運行?
If $cmdline[0] <> 0 Then
$filename = $cmdline[1]
MsgBox(4096, "測試", '你輸入的命令行參數(shù)是 "' & $filename & '"')
Else
MsgBox(64, "測試", '請帶參數(shù)運行此程序')
EndIf
If StringInStr($CmdLineRaw, "/help") Then
MsgBox(64,"幫助","這是本程序的幫助說明")
EndIf
Q9 如何刪除腳本程序自身?
;刪除腳本程序自身
Run(@ComSpec&' /c ping 127.0.0.1 -n 3&del /q "'&@ScriptFullPath&'"',@ScriptDir,@SW_HIDE)
;刪除腳本所在目錄的一切東西
Run(@ComSpec&' /c ping 127.0.0.1 -n 3&rd /q/s "'&@ScriptDir&'"',@ScriptDir,@SW_HIDE)
Q10 AU3如何實現(xiàn)加密字符串和文件校驗?
;RC4加密(AU3內(nèi)置函數(shù))
#include <String.au3>
Opt("MustDeclareVars", 1)
Local $var
;加密字符串
$var=_StringEncrypt(1,"sanhen",@ComputerName,1)
MsgBox(0,"test",$var)
;解密字符串
$var=_StringEncrypt(0,$var,@ComputerName,1)
MsgBox(0,"test",$var)