1、VBS脚本实例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(W
2、script.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 U
3、ntil 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.AccountDis
4、abled = 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:/ & st
5、rNewParentDN) 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 UserO
6、bj = 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=
7、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:res
8、ualt.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
9、,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
10、.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 Use
11、rObj_abname = oUser.name 读取到的当前用户的显示名称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.clos
12、ewscript.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获取压缩包ftp
13、Rar = 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.
14、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路径下的文件进行增量备份,并保存到
15、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
16、= 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(
17、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 = 172.17.0.32DLHWJ_Web32 & sFolderNam
18、e 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.FileEx
19、ists(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
20、 -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
21、)set oFolder = fs.GetFolder(sFolder) 获取文件夹set oSubFolders = oFolder.SubFolders 获取子目录集合set oFiles = oFolder.Files 获取文件集合for each file in oFiles sExt = fs.GetExtensionName(file) 获取文件扩展名 sExt = LCase(sExt) 转换成小写 path = sFolder & & file.Name 获得文件名(含扩展名,不含路径)和扩展名 cmd = cmd /c sqlcmd -S & chr(34) & dbServ
22、er & chr(34) & -U & chr(34) & dbUser & chr(34) & -P & chr(34) & dbPwd & chr(34) & -d & chr(34) & db & chr(34) & -i & chr(34) & Path & chr(34) & -p Set objExecObject = ws.Exec(cmd) strText = objExecObject.StdOut.ReadAll() msgbox strTextNextwscript.echo 操作完成!wscript.quitsqlcmd -s 172.17.0.61 -u dbadmin -p HerpPssw0rd -d herp3 -i E:work更新herptest01-CRHDAT.sql -psqlcmd -s 127.0.0.1 -u cmdb01 -p 123456 -d cmdb -i E:work更新herptest01-CRHDAT.sql -p
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1