VBS脚本实例
说明:
本文中所列出的脚本实例,均为本人因日常工作需要所写,已在生产环境中验证并使用(参数请自行修改),希望可以帮到有需要的人。但本人并非非常熟悉VBS脚本,属于现学现卖的,欢迎批评指正或是与VBS/PowerShell脚本相关的技术交流。谢谢!
1 AD相关
1.1 迁移用户/计算机到指定OU
**************************************************************************************
on error resume next
Set 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.ReadLine
comm = "cmd /c echo " & base & " >>C:\resualt.txt"
WSshell.run comm,0,true
If fso.fileexists("C:\" & base & ".txt") Then '判断是否有指定OU的txt文件
Set file=fso.OpenTextFile("C:\" & base & ".txt",1) '打开文件
Count = 0
ErrorCount = 0
disabledCount = 0
Do Until file.AtEndOfLine=-1
name = file.ReadLine 'name 形如:cn=test123,ou=test,dc=abc,dc=com ab
group = Right(name,2)
Err.Number = 0
If LCase(group) = "ab" Then
name = Left(name,Len(name)-3)
'wscript.echo name
Set UserObj = GetObject("LDAP://" & name)
'wscript.echo err.number
If Err.Number = 0 Then
If UserObj.AccountDisabled = FALSE Then
strNewParentDN = "ou=" & base & ",ou=personnel,ou=ab,DC=abc,DC=com"
set objCont = GetObject("LDAP://" & strNewParentDN)
objCont.MoveHere "LDAP://" & name,vbNullString
Count_dc = Count_dc + 1
else
strNewParentDN = "ou=personel,ou=disabledAccounts,DC=abc,DC=com"
set objCont = GetObject("LDAP://" & strNewParentDN)
objCont.MoveHere "LDAP://" & name,vbNullString
disabledCount = disabledCount + 1
End If
Else
ErrorCount = ErrorCount + 1
comm = "cmd /c echo dc >>C:\resualt.txt"
WSshell.run comm,0,true
comm = "cmd /c echo " & name & " >>C:\resualt.txt"
WSshell.run comm,0,true
End If
Else
name = Left(name,Len(name)-6)
Set UserObj = GetObject("LDAP://" & name)
If Err.Number = 0 Then '
If UserObj.AccountDisabled = FALSE Then
strNewParentDN = "ou=" & base & ",ou=personnel,ou=ab,DC=abc,DC=com"
set objCont = GetObject("LDAP://" & strNewParentDN)
objCont.MoveHere "LDAP://" & name,vbNullString
Count_dcits = Count_dcits + 1
else
strNewParentDN = "ou=personel,ou=disabledAccounts,DC=abc,DC=com"
set objCont = GetObject("LDAP://" & strNewParentDN)
objCont.MoveHere "LDAP://" & name,vbNullString
disabledCount = disabledCount + 1
End If
Else
comm = "cmd /c echo dcits >>C:\resualt.txt"
WSshell.run comm,0,true
ErrorCount = ErrorCount + 1
comm = "cmd /c echo " & name & " >>C:\resualt.txt"
WSshell.run comm,0,true
End If
End If
Loop
file.close
comm = "cmd /c echo Count_dc=" & Count_dc & " >>C:\resualt.txt"
WSshell.run comm,0,true
comm = "cmd /c echo Count_dcits=" & Count_dcits & " >>C:\resualt.txt"
WSshell.run comm,0,true
comm = "cmd /c echo disabledCount=" & disabledCount & " >>C:\resualt.txt"
WSshell.run comm,0,true
comm = "cmd /c echo ErrorCount=" & ErrorCount & " >>C:\resualt.txt"
WSshell.run comm,0,true
End If
Loop
basefile.close
wscript.echo "操作完成"
Wscript.Quit
******************************************************************************
1.2 批量导出AD中指定OU的用户和安全组
**************************************************************************************
'读取abc.com域下abOU下,personnelOU中所有OU中的用户和安全组
'结果保存到C:\personnel-ab.txt
**************************************************************************************
on error resume next
Set 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 = -1
base = basefile.ReadLine
ou1 = "ou=" & base & ",ou=personnel,ou=ab,dc=abc,dc=com"
Set UserObj_ab = GetObject("LDAP://" & ou1)
For Each oUser in UserObj_ab
name = 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.txt"
WSshell.run comm,0,true '输出到指定文件夹
Next
Loop
basefile.close
wscript.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_day
End If
If Len(temp_month) = 1 Then
temp_month = "0" & temp_month
End If
today = temp_year & temp_month & temp_day
'msgbox today
'获取压缩包
ftpRar = "D:\FTP\HERP3.1"
uFolder = "D:\HERP3.1\update\" & temp_year
count = 0
Set wsShell= wscript.CreateObject("wscript.shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(ftpRar)
'Set oSubFolders = oFolder.SubFolders
For 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
Next
If count = 0 Then
Msgbox "压缩包不存在!"
Wscript.quit
End 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_day
End If
If Len(temp_month) = 1 Then
temp_month = "0" & temp_month
End If
today = temp_year & temp_month & temp_day
'增量备份
count = 0
uFolder = "D:\HERP3.1\update\" & temp_year
newPath = "D:\HERP3.1\backup\" & temp_year & "\" & today
Set fso = CreateObject("Scripting.FileSystemObject")
Set WsShell = WScript.CreateObject("wscript.shell")
Set oFolder = fso.GetFolder(uFolder)
Set oSubFolders = oFolder.SubFolders
For 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 = "\\172.17.0.32\DLHWJ_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 If
Next
Msgbox "已备份" & count & "个对象。备份完成!"
Wscript.Quit
*******************************************************************************
2 数据库相关
2.3 批量执行SQL脚本
*******************************************************************************
‘数据库为SQL Server 2008/2012,其他版本的数据库没有尝试过。
‘批量执行指定路径(sFolder)下的sql脚本,提示执行结果。
'sqlcmd命令
格式
pdf格式笔记格式下载页码格式下载公文格式下载简报格式下载
:sqlcmd -s "服务器IP" -u "数据库用户名" -p "用户密码" -d "数据库名" -i "sql脚本完全路径" -p
*******************************************************************************
Set ws=WScript.CreateObject("wscript.shell")
sFolder = "E:\work\sql" #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.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) & dbServer & 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 strText
Next
wscript.echo "操作完成!"
wscript.quit
'sqlcmd -s "172.17.0.61" -u "dbadmin" -p "HerpP@ssw0rd" -d "herp3" -i "E:\work\更新\herp\test\01-CRHDAT.sql" -p
'sqlcmd -s "127.0.0.1" -u "cmdb01" -p "123456" -d "cmdb" -i "E:\work\更新\herp\test\01-CRHDAT.sql" -p