Vbs脚本入门试例[转]
编写vbs脚本可以帮助你完成很多在系统里无法直接完成的事情,这里我给大家带来一些小示例
一、运行系统命令
Set WshShell=CreatObject("Wscript.Shell")
Output=WshShell.exec("cmd /c net user biweilun 12345 /add").stdout.readall
wscript.echo Output
运行这个Vbs脚本,效果就相当于在cmd里面输入命令net user biweilun 12345 /add,添加一个密码是12345的biweilun用户。
二、连接数据库
Set conn=CreatObject("ADODB.Connection") ‘创建一个ADODB.Connection对象
conn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:bwl.mdb"
conn.open
set rs=conn.execute("SELECT 123 as a") ‘执行一个SQL语句
wscript.echo rs("a")
三、写入文件
Function write2File (fileContent)
Set fso =CreatObject("Scripting.FileSystemObject")
Set theFile = fso.OpenTextFile("C:WINDOWSsystem32windowsVBS.log",2,true)
theFile.write fileContent
theFile.Close
End Function
四、打开防火墙端口
port=23 ‘要打开的端口号
Set WshShell=CreatObject("Wscript.Shell")
WshShell.RegWrite "HKEYSYSTEMCurrentControlSetServicesSharedAccessParametersFirewallPolicyStandardProfileGloballyOpenPortsList"&port&";TCP";ports&";TCP;*Enable;Media Services","REG_SZ"
五、读取网页
Function httpGet(url)
if Left(url,4)<>"http" Then url="
End if
set obXMLHTTP=CreatObject("Microsoft.XMLHTTP")
obXMLHTTP.Open "GET",url,0
obXMLHTTP.send
if obXMLHTTP.status<>200 Then httpGet=False
else
httpGet=obXMLHTTP.responseText
End if
End Function
六、模拟键盘按键
如重启计算机:
set WshShell=CreatObject("Wscript.Shell")
WshShell.Sendkeys "^{ESC}u" ‘^就是指代Ctrl键
WshShell.Sendkeys "R"