VBS脚本实例.docx_第1页
VBS脚本实例.docx_第2页
VBS脚本实例.docx_第3页
VBS脚本实例.docx_第4页
VBS脚本实例.docx_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

VBS脚本实例说明:本文中所列出的脚本实例,均为本人因日常工作需要所写,已在生产环境中验证并使用(参数请自行修改),希望可以帮到有需要的人。但本人并非非常熟悉VBS脚本,属于现学现卖的,欢迎批评指正或是与VBS/PowerShell脚本相关的技术交流。谢谢!1 AD相关1.1 迁移用户/计算机到指定OU*on error resume nextSet fso = CreateObject(Scripting.FileSystemObject)Set basefile =fso.OpenTextFile(C:base.txt,1)Set WSshell=CreateObject(Wscript.shell)Do Until basefile.AtEndOfLine = -1读取txt文件,直到最后一行结束base = basefile.ReadLinecomm = cmd /c echo & base & C:resualt.txtWSshell.run comm,0,trueIf fso.fileexists(C: & base & .txt) Then判断是否有指定OU的txt文件Set file=fso.OpenTextFile(C: & base & .txt,1) 打开文件Count = 0ErrorCount = 0disabledCount = 0Do Until file.AtEndOfLine=-1 name = file.ReadLine name 形如:cn=test123,ou=test,dc=abc,dc=comabgroup = Right(name,2)Err.Number = 0If LCase(group) = ab Thenname = Left(name,Len(name)-3)wscript.echo name Set UserObj = GetObject(LDAP:/ & name)wscript.echo err.numberIf Err.Number = 0 Then If UserObj.AccountDisabled = FALSE ThenstrNewParentDN = ou= & base & ,ou=personnel,ou=ab,DC=abc,DC=com set objCont = GetObject(LDAP:/ & strNewParentDN) objCont.MoveHere LDAP:/ & name,vbNullStringCount_dc = Count_dc + 1 elsestrNewParentDN = ou=personel,ou=disabledAccounts,DC=abc,DC=com set objCont = GetObject(LDAP:/ & strNewParentDN) objCont.MoveHere LDAP:/ & name,vbNullStringdisabledCount = disabledCount + 1 End IfElseErrorCount = ErrorCount + 1comm = cmd /c echo dc C:resualt.txtWSshell.run comm,0,truecomm = cmd /c echo & name & C:resualt.txtWSshell.run comm,0,trueEnd IfElse name = Left(name,Len(name)-6) Set UserObj = GetObject(LDAP:/ & name)If Err.Number = 0 ThenIf UserObj.AccountDisabled = FALSE ThenstrNewParentDN = ou= & base & ,ou=personnel,ou=ab,DC=abc,DC=com set objCont = GetObject(LDAP:/ & strNewParentDN) objCont.MoveHere LDAP:/ & name,vbNullStringCount_dcits = Count_dcits + 1 elsestrNewParentDN = ou=personel,ou=disabledAccounts,DC=abc,DC=com set objCont = GetObject(LDAP:/ & strNewParentDN) objCont.MoveHere LDAP:/ & name,vbNullStringdisabledCount = disabledCount + 1 End IfElsecomm = cmd /c echo dcits C:resualt.txtWSshell.run comm,0,trueErrorCount = ErrorCount + 1comm = cmd /c echo & name & C:resualt.txtWSshell.run comm,0,trueEnd IfEnd IfLoopfile.closecomm = cmd /c echo Count_dc= & Count_dc & C:resualt.txtWSshell.run comm,0,truecomm = cmd /c echo Count_dcits= & Count_dcits & C:resualt.txtWSshell.run comm,0,truecomm = cmd /c echo disabledCount= & disabledCount & C:resualt.txtWSshell.run comm,0,truecomm = cmd /c echo ErrorCount= & ErrorCount & C:resualt.txtWSshell.run comm,0,trueEnd IfLoopbasefile.closewscript.echo 操作完成Wscript.Quit*1.2 批量导出AD中指定OU的用户和安全组*读取域下abOU下,personnelOU中所有OU中的用户和安全组结果保存到C:personnel-ab.txt*on error resume nextSet fso = CreateObject(Scripting.FileSystemObject) 用base.txt存放ou=personnel,ou=ab,dc=abc,dc=com下各OU的名称Set basefile =fso.OpenTextFile(C:base.txt,1) Set WSshell=CreateObject(Wscript.shell)Do Until basefile.AtEndOfLine = -1base = basefile.ReadLineou1 = ou= & base & ,ou=personnel,ou=ab,dc=abc,dc=comSet UserObj_ab = GetObject(LDAP:/ & ou1)For Each oUser in UserObj_abname = oU 读取到的当前用户的显示名称account = oUser.sAMAccountName 用户的登录名disabled = oUser.AccountDisabled 用户状态,True为禁用,False为启用dn = oUser.distinguishedName用户LDAP路径comm = cmd /c echo & name & & account & & base & & disabled & dc C:personnel-ab.txtWSshell.run comm,0,true输出到指定文件夹NextLoopbasefile.closewscript.echo 操作完成Wscript.Quit*2 文件相关2.1 解压文件到指定文件夹获取当前日期temp_day = day(date)temp_month = month(date)temp_year = year(date)If Len(temp_day) = 1 Then temp_day = 0 & temp_dayEnd IfIf Len(temp_month) = 1 Then temp_month = 0 & temp_monthEnd Iftoday = temp_year & temp_month & temp_daymsgbox today获取压缩包ftpRar = D:FTPHERP3.1uFolder = D:HERP3.1update & temp_yearcount = 0Set wsShell= wscript.CreateObject(wscript.shell)Set fso = CreateObject(Scripting.FileSystemObject)Set oFolder = fso.GetFolder(ftpRar) Set oSubFolders = oFolder.SubFoldersFor Each File in oFolder.Files rarName = File.Name rarPath = File.Path msgbox rarName msgbox rarPath If Left(rarName,8) = today Then count = count + 1 解压缩 cmd = winrar x -t -o+ -p- & rarPath & chr(32) & uFolder msgbox cmd wsShell.Run cmd End If NextIf count = 0 Then Msgbox 压缩包不存在! Wscript.quitEnd If*2.2 增量备份*用于文件更新前的增量备份。根据uFolder路径下的文件,对bakSource路径下的文件进行增量备份,并保存到newPath路径下。*获取当前日期temp_day = day(date)temp_month = month(date)temp_year = year(date)If Len(temp_day) = 1 Then temp_day = 0 & temp_dayEnd IfIf Len(temp_month) = 1 Then temp_month = 0 & temp_monthEnd Iftoday = temp_year & temp_month & temp_day增量备份count = 0 uFolder = D:HERP3.1update & temp_yearnewPath = D:HERP3.1backup & temp_year & & todaySet fso = CreateObject(Scripting.FileSystemObject)Set WsShell = WScript.CreateObject(wscript.shell)Set oFolder = fso.GetFolder(uFolder) Set oSubFolders = oFolder.SubFoldersFor Each Folder in oSubFolders FolderName = Folder.Name FolderPath = Folder.Path If Left(FolderName,8) = today Then If fso.FolderExists(newPath) Then Else Set newFolder = fso.CreateFolder(newPath) End If Set sFolders = fso.GetFolder(FolderPath) Set sSubFolders = sFolders.SubFolders For Each sFolder in sSubFolders sFolderName = sFolder.Name bakSource = 2DLHWJ_Web32 & sFolderName bakDestination = newPath & & sFolderName If fso.FolderExists(bakSource) Then count = count + 1 cmd = cmd /c xcopy /y /e & bakSource & chr(32) & bakDestination WsShell.Run cmd,1,true End If Next For Each File in sFolders.Files bakSource = 网络IP共享文件夹 & File.Name bakDestination = newPath If fso.FileExists(bakSource) Then count = count + 1 cmd = cmd /c xcopy /y /e & bakSource & chr(32) & bakDestination WsShell.Run cmd,1,true End If Next End IfNextMsgbox 已备份 & count & 个对象。备份完成!Wscript.Quit*3 数据库相关3.1 批量执行SQL脚本*数据库为SQL Server 2008/2012,其他版本的数据库没有尝试过。批量执行指定路径(sFolder)下的sql脚本,提示执行结果。sqlcmd命令格式:sqlcmd -s 服务器IP -u 数据库用户名 -p 用户密码 -d 数据库名 -i sql脚本完全路径 -p*Set ws=WScript.CreateObject(wscript.shell)sFolder = E:worksql #sql脚本存放路径dbServer = test-db #数据库服务器的计算机名/IP地址(计算机名需要考虑远程域名解析的问题)dbUser = db-user #访问数据库的用户名,域用户为“域用户名”dbPwd = user-pwd #用户密码db = test #库名set fs = CreateObject(Scripting.FileSystemObject)set oFolder = fs.GetFolder(sFolder) 获取文件夹set oSubFolders = oFolder.Su

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论