发新话题
打印

[转载]备份 恢复 修改IIS站点IP的小脚本工具

[转载]备份 恢复 修改IIS站点IP的小脚本工具

本文作者: blackfxo
信息来源:SuperHei

备份,恢复,修改IIS站点IP的小脚本 感谢EMM和LUOLUO的帮助,让我完成了这个小工具,发出来大家指教下,谢谢
复制内容到剪贴板
代码:
dim siteindexdim backupfileset arg=wscript.Argumentsif arg.count<=0 thenwscript.echo "briis.vbs backup backupfile.txt"wscript.echo "briis.vbs restore backupfile.txt"wscript.echo "briis.vbs changeip"wscript.quitend if

select case arg(0)case "backup"backupfile=arg(1)BackupSite()case "restore"backupfile=arg(1)RestoreSite()case "changeip"ChangeSiteIP()end select

function BackupSite()siteindex=1 &#39;站点ID的索引&#39;*********************************************&#39;把站点的ID读入数组siteid方便下面修改&#39;ChildObject.AdsPath IIS虚拟站点路径,对于第一个虚拟站点,这个值=IIS://localhost/w3svc/1,最后那个1是指虚拟站点的ID号。Set w3svc = GetObject("IIS://localhost/w3svc") &#39;建立IIS对象For Each ChildObject In w3svcIf (Err.Number <> 0) Then Exit ForChildObjectName = Replace(ChildObject.AdsPath, Left(ChildObject.AdsPath, 22), "") &#39;过滤掉IIS虚拟路径前面的字符,把最后一位,也就是站点ID号取出来If IsNumeric(ChildObjectName) = True Then &#39;判断取出的ChildObjectName是不是数字,如果为真的话,就把值赋给siteid这个数组Set iis = w3svc.GetObject("IIsWebServer", ChildObjectName)If Err.Number <> 0 ThenExit ForMsgBox ("出错")wscript.quitEnd IfServerBindings = iis.ServerBindings &#39;把IIS虚拟站点绑定的IP、端口、域名的值放入数组ServerBindingsServerComment = iis.ServerComment &#39;把IIS虚拟站点的名称赋值给变量ServerCommentMaxConnections = iis.MaxConnections &#39;把IIS虚拟站点的最大连接数赋值给变量MaxConnectionsset IIsWeb=iis.GetObject("IIsWebVirtualDir","Root")Path=IIsWeb.Pathset fso=CreateObject("scripting.filesystemObject")if (fso.fileexists(backupfile)) thenset flo=fso.OpenTextFile(backupfile,8,True)flo.WriteLine(""&ServerComment&vbTab&join(ServerBindings,",")&vbTab&MaxConnections&vbTab&Path)flo.closewscript.echo "正在备份站点 "&ServerComment&"!"elseset flo=fso.CreateTextFile(backupfile,true)flo.WriteLine(""&ServerComment&vbTab&join(ServerBindings,",")&vbTab&MaxConnections&vbTab&Path)flo.closewscript.echo "正在备份站点 "&ServerComment&"!"end ifsiteindex = siteindex + 1End IfNextwscript.echo "一共备份了 "&siteindex-1&"个站点!"&vbcrlfend function

function RestoreSite()set fso=CreateObject("scripting.filesystemObject")if (fso.fileexists(backupfile)) thenelsemsgbox "没有找到文件 "&backupfile&"!"&vbcrlf&"请确定文件的位置!"wscript.quitend ifset fl=fso.OpenTextFile(backupfile,1)do while fl.AtEndOfStream<>Truefline=fl.readlinefll=split(fline,vbTab)crwebstat=CreateWebServer(fll(0),split(fll(1),","),fll(2),fll(3))if crwebstat=0 thenwscript.echo "创建站点"&fll(0)&"失败!"elseif crwebstat=1 thenwscript.echo "创建站点"&fll(0)&"成功!"end ifloopend function

Function ChangeSiteIP()Dim siteid(1000),oldip,newip,w3svc,iis&#39;*********************************************&#39;获取两个IP,一个是原来的,是个是新的oldip=inputbox("请输入原来的IP")if oldip="" thenmsgbox ("输入错误") &#39;判断是否输入为空wscript.quitend ifnewip=inputbox("请输入新的IP")if isnull(newip) or newip="" thenmsgbox ("输入错误") &#39;判断是否输入为空wscript.quitend if&#39;*********************************************

&#39;*********************************************&#39;把站点的ID读入数组siteid方便下面修改&#39;ChildObject.AdsPath IIS虚拟站点路径,对于第一个虚拟站点,这个值=IIS://localhost/w3svc/1,最后那个1是指虚拟站点的ID号。Set w3svc = GetObject("IIS://localhost/w3svc") &#39;建立IIS对象For Each ChildObject In w3svcIf (Err.Number <> 0) Then Exit ForChildObjectName = Replace(ChildObject.AdsPath, Left(ChildObject.AdsPath, 22), "") &#39;过滤掉IIS虚拟路径前面的字符,把最后一位,也就是站点ID号取出来If IsNumeric(ChildObjectName) = True Then &#39;判断取出的ChildObjectName是不是数字,如果为真的话,就把值赋给siteid这个数组Set iis = w3svc.GetObject("IIsWebServer", ChildObjectName) &#39;建立IIS虚拟站点对像,以便对站点ID号=id的虚拟站点的属性进行操作If Err.Number <> 0 ThenExit ForMsgBox ("出错")wscript.quitEnd IfServerBindings = iis.ServerBindings &#39;把IIS虚拟站点绑定的IP、端口、域名的值放入数组ServerBindingsServerComment = iis.ServerComment &#39;把IIS虚拟站点的名称赋值给变量ServerCommentwscript.echo "正在修改站点 "&ServerComment&" 的IP" &#39;在命令行打印正在修改IP的站点名称bindsindex = UBound(ServerBindings) + 1 &#39;由于站点可以绑定多个域名、端口和IP,所以需要判断一共绑定了多少IPFor bindsarrid = 1 To bindsindexServerBindings(bindsarrid-1) = Replace(ServerBindings(bindsarrid-1),oldip,newip) &#39;把数组内包含oldip的字符串替换成newip的字符串Nextiis.ServerBindings = ServerBindings &#39;把iis对象的ServerBindings属性修改为替换过IP的数组iis.setinfo &#39;使替换过的设置生效End IfNext&#39;*********************************************set w3svc=nothingset iis=nothingmsgbox "修改完成!"End Function

Function CreateWebServer(ServerComment,Sbindings,MaxConnections,Path) On Error Resume Next Dim ServiceObj,ServerObj,VDirObj Set ServiceObj = GetObject("IIS://localhost/W3SVC")&#39;&#39; 首先创建一个服务实例

WNumber=1 Do While IsObject(ServiceObj.GetObject("IIsWebServer",WNumber)) If Err.number<>0 Then Err.Clear() Exit Do End If WNumber=WNumber+1 Loop

Set ServerObj = ServiceObj.Create("IIsWebServer", WNumber)&#39;&#39; 然后创建一个WEB服务器

If (Err.Number <> 0) Then&#39;&#39; 是否出错 msgbox "错误: 创建Web服务器的ADSI操作失败!" CreateWebServer=0 Exit Function End If

&#39;&#39; 接着配置服务器 ServerObj.ServerSize = 1 &#39;&#39; 中型大小 ServerObj.ServerComment = ServerComment &#39;&#39;说明 ServerObj.ServerBindings = Sbindings &#39;&#39;端口 ServerObj.MaxConnections = MaxConnections &#39;最大连接数ServerObj.EnableDefaultDoc=True

&#39;&#39; 提交信息 ServerObj.SetInfo

&#39;&#39; 最后,建立虚拟目录 Set VDirObj = ServerObj.Create("IIsWebVirtualDir", "Root")

&#39;If (Err.Number <> 0) Then&#39;&#39; 是否出错 &#39; msgbox "错误: 创建虚拟目录的ADSI操作失败!" &#39; CreateWebServer=0 &#39; Exit Function &#39;End If

&#39;&#39; 配置虚拟目录 VDirObj.Path = Path VDirObj.AccessRead = True VDirObj.AccessWrite = FalseVDirObj.EnableDirBrowsing = False VDirObj.EnableDefaultDoc=True VDirObj.AccessScript=True VDirObj.AppCreate2 2VDirObj.AppFriendlyName="默认应用程序" VDirObj.SetInfo

Set VDirObj=Nothing Set ServerObj=Nothing Set ServiceObj=Nothing CreateWebServer=1 End Function&#39;本代码参考了源码之家的《用ASP编程控制IIS建立WEB站点》和Adsutil.vbs的代码&#39;还要感谢Envymask和LuoLuo的帮助,谢谢&#39;小生对ADSI也不是很熟悉,所以代码中有什么错误的,还请大家批评指教,谢谢

TOP

发新话题