VBS脚本实例.docx

上传人:b****5 文档编号:11767657 上传时间:2023-04-01 格式:DOCX 页数:10 大小:17.58KB
下载 相关 举报
VBS脚本实例.docx_第1页
第1页 / 共10页
VBS脚本实例.docx_第2页
第2页 / 共10页
VBS脚本实例.docx_第3页
第3页 / 共10页
VBS脚本实例.docx_第4页
第4页 / 共10页
VBS脚本实例.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

VBS脚本实例.docx

《VBS脚本实例.docx》由会员分享,可在线阅读,更多相关《VBS脚本实例.docx(10页珍藏版)》请在冰豆网上搜索。

VBS脚本实例.docx

VBS脚本实例

VBS脚本实例

说明:

本文中所列出的脚本实例,均为本人因日常工作需要所写,已在生产环境中验证并使用(参数请自行修改),希望可以帮到有需要的人。

但本人并非非常熟悉VBS脚本,属于现学现卖的,欢迎批评指正或是与VBS/PowerShell脚本相关的技术交流。

谢谢!

1AD相关

1.1迁移用户/计算机到指定OU

**************************************************************************************

 onerrorresumenext

Setfso=CreateObject("Scripting.FileSystemObject")

Setbasefile=fso.OpenTextFile("C:

\base.txt",1) 

SetWSshell=CreateObject("Wscript.shell") 

Do Until basefile.AtEndOfLine=-1  '读取txt文件,直到最后一行结束

 base=basefile.ReadLine

 comm="cmd/cecho"&base&">>C:

\resualt.txt"

 WSshell.runcomm,0,true

 Iffso.fileexists("C:

\"&base&".txt")Then   '判断是否有指定OU的txt文件

  Setfile=fso.OpenTextFile("C:

\"&base&".txt",1)  '打开文件

  Count=0

  ErrorCount=0 

  disabledCount=0

  Do Untilfile.AtEndOfLine=-1

     name=file.ReadLine  'name形如:

cn=test123,ou=test,dc=abc,dc=com ab

   group=Right(name,2)

   Err.Number=0

   IfLCase(group)="ab"Then

    name=Left(name,Len(name)-3)  

    'wscript.echoname

      SetUserObj=GetObject("LDAP:

//"&name) 

    'wscript.echoerr.number

    IfErr.Number=0Then

       IfUserObj.AccountDisabled=FALSEThen 

      strNewParentDN="ou="&base&",ou=personnel,ou=ab,DC=abc,DC=com"

        setobjCont=GetObject("LDAP:

//"&strNewParentDN)

        objCont.MoveHere"LDAP:

//"&name,vbNullString

      Count_dc=Count_dc+1

       else 

      strNewParentDN="ou=personel,ou=disabledAccounts,DC=abc,DC=com"

        setobjCont=GetObject("LDAP:

//"&strNewParentDN)

        objCont.MoveHere"LDAP:

//"&name,vbNullString

      disabledCount=disabledCount+1

       EndIf

    Else 

     ErrorCount=ErrorCount+1

     comm="cmd/cechodc>>C:

\resualt.txt"

     WSshell.runcomm,0,true

     comm="cmd/cecho"&name&">>C:

\resualt.txt"

     WSshell.runcomm,0,true

    EndIf

   Else 

    name=Left(name,Len(name)-6)

      SetUserObj=GetObject("LDAP:

//"&name) 

    IfErr.Number=0Then '

        IfUserObj.AccountDisabled=FALSEThen 

      strNewParentDN="ou="&base&",ou=personnel,ou=ab,DC=abc,DC=com"

        setobjCont=GetObject("LDAP:

//"&strNewParentDN)

        objCont.MoveHere"LDAP:

//"&name,vbNullString

      Count_dcits=Count_dcits+1

       else 

      strNewParentDN="ou=personel,ou=disabledAccounts,DC=abc,DC=com"

        setobjCont=GetObject("LDAP:

//"&strNewParentDN)

        objCont.MoveHere"LDAP:

//"&name,vbNullString

      disabledCount=disabledCount+1

       EndIf

    Else 

     comm="cmd/cechodcits>>C:

\resualt.txt"

     WSshell.runcomm,0,true

     ErrorCount=ErrorCount+1

     comm="cmd/cecho"&name&">>C:

\resualt.txt"

     WSshell.runcomm,0,true

    EndIf

   EndIf

  Loop

  file.close

  comm="cmd/cechoCount_dc="&Count_dc&">>C:

\resualt.txt"

  WSshell.runcomm,0,true

  comm="cmd/cechoCount_dcits="&Count_dcits&">>C:

\resualt.txt"

  WSshell.runcomm,0,true

  comm="cmd/cechodisabledCount="&disabledCount&">>C:

\resualt.txt"

  WSshell.runcomm,0,true

  comm="cmd/cechoErrorCount="&ErrorCount&">>C:

\resualt.txt"

  WSshell.runcomm,0,true

 EndIf

Loop

basefile.close

wscript.echo"操作完成"

Wscript.Quit

******************************************************************************

1.2批量导出AD中指定OU的用户和安全组

**************************************************************************************

'读取域下abOU下,personnelOU中所有OU中的用户和安全组

'结果保存到C:

\personnel-ab.txt

**************************************************************************************

onerrorresumenext

Setfso=CreateObject("Scripting.FileSystemObject")

'用base.txt存放ou=personnel,ou=ab,dc=abc,dc=com下各OU的名称

Setbasefile=fso.OpenTextFile("C:

\base.txt",1)

SetWSshell=CreateObject("Wscript.shell") 

Do Until basefile.AtEndOfLine=-1

 base=basefile.ReadLine

 ou1="ou="&base&",ou=personnel,ou=ab,dc=abc,dc=com"

 SetUserObj_ab=GetObject("LDAP:

//"&ou1) 

 ForEachoUserinUserObj_ab

  name=oUser.name '读取到的当前用户的显示名称

  account=oUser.sAMAccountName'用户的登录名

  disabled=oUser.AccountDisabled'用户状态,True为禁用,False为启用

'  dn=oUser.distinguishedName  '用户LDAP路径

  comm="cmd/cecho"&name&""&account&""&base&""&disabled&"dc>>C:

\personnel-ab.txt"

  WSshell.runcomm,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)

IfLen(temp_day)=1Then

temp_day="0"&temp_day

EndIf

IfLen(temp_month)=1Then

temp_month="0"&temp_month

EndIf

today=temp_year&temp_month&temp_day

'msgboxtoday

 

'获取压缩包

ftpRar="D:

\FTP\HERP3.1"

uFolder="D:

\HERP3.1\update\"&temp_year

count=0

SetwsShell=wscript.CreateObject("wscript.shell")

Setfso=CreateObject("Scripting.FileSystemObject")

SetoFolder=fso.GetFolder(ftpRar)

'SetoSubFolders=oFolder.SubFolders

ForEachFileinoFolder.Files

rarName=File.Name

rarPath=File.Path

'msgboxrarName

'msgboxrarPath

IfLeft(rarName,8)=todayThen

count=count+1

'解压缩

cmd="winrarx-t-o+-p-"&rarPath&chr(32)&uFolder

'msgboxcmd

wsShell.Runcmd

EndIf

Next

Ifcount=0Then

Msgbox"压缩包不存在!

"

Wscript.quit

EndIf

*******************************************************************************

2.2增量备份

*******************************************************************************

用于文件更新前的增量备份。

根据uFolder路径下的文件,对bakSource路径下的文件进行增量备份,并保存到newPath路径下。

*******************************************************************************

'获取当前日期

temp_day=day(date)

temp_month=month(date)

temp_year=year(date)

IfLen(temp_day)=1Then

temp_day="0"&temp_day

EndIf

IfLen(temp_month)=1Then

temp_month="0"&temp_month

EndIf

today=temp_year&temp_month&temp_day

'增量备份

count=0

uFolder="D:

\HERP3.1\update\"&temp_year

newPath="D:

\HERP3.1\backup\"&temp_year&"\"&today

Setfso=CreateObject("Scripting.FileSystemObject")

SetWsShell=WScript.CreateObject("wscript.shell")

SetoFolder=fso.GetFolder(uFolder)

SetoSubFolders=oFolder.SubFolders

ForEachFolderinoSubFolders

FolderName=Folder.Name

FolderPath=Folder.Path

IfLeft(FolderName,8)=todayThen

Iffso.FolderExists(newPath)Then

Else

SetnewFolder=fso.CreateFolder(newPath)

EndIf

SetsFolders=fso.GetFolder(FolderPath)

SetsSubFolders=sFolders.SubFolders

ForEachsFolderinsSubFolders

sFolderName=sFolder.Name

bakSource="\\172.17.0.32\DLHWJ_Web32\"&sFolderName

bakDestination=newPath&"\"&sFolderName

Iffso.FolderExists(bakSource)Then

count=count+1

cmd="cmd/cxcopy/y/e"&bakSource&chr(32)&bakDestination

WsShell.Runcmd,1,true

EndIf

Next

ForEachFileinsFolders.Files

bakSource="\\网络IP\共享文件夹\"&File.Name

bakDestination=newPath

'Iffso.FileExists(bakSource)Then

count=count+1

cmd="cmd/cxcopy/y/e"&bakSource&chr(32)&bakDestination

WsShell.Runcmd,1,true

'EndIf

Next

EndIf

Next

Msgbox"已备份"&count&"个对象。

备份完成!

"

Wscript.Quit

*******************************************************************************

3数据库相关

3.1批量执行SQL脚本

*******************************************************************************

‘数据库为SQLServer2008/2012,其他版本的数据库没有尝试过。

‘批量执行指定路径(sFolder)下的sql脚本,提示执行结果。

'sqlcmd命令格式:

sqlcmd-s"服务器IP"-u"数据库用户名"-p"用户密码"-d"数据库名"-i"sql脚本完全路径"-p

*******************************************************************************

Setws=WScript.CreateObject("wscript.shell")

sFolder="E:

\work\sql"#sql脚本存放路径

dbServer="test-db"#数据库服务器的计算机名/IP地址(计算机名需要考虑远程域名解析的问题)

dbUser="db-user"#访问数据库的用户名,域用户为“域\用户名”

dbPwd="user-pwd"#用户密码

db="test"#库名

setfs=CreateObject("Scripting.FileSystemObject")

setoFolder=fs.GetFolder(sFolder)'获取文件夹

setoSubFolders=oFolder.SubFolders'获取子目录集合

setoFiles=oFolder.Files'获取文件集合

foreachfileinoFiles

sExt=fs.GetExtensionName(file)'获取文件扩展名

sExt=LCase(sExt)'转换成小写

path=sFolder&"\"&file.Name'获得文件名(含扩展名,不含路径)和扩展名

cmd="cmd/csqlcmd-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"

SetobjExecObject=ws.Exec(cmd)

strText=objExecObject.StdOut.ReadAll()

msgboxstrText

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

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1