当前位置:web 服务器 > IIS >>

备份.恢复.修改IIS站点IP的脚本

备份,恢复,修改IIS站点IP的小脚本
感谢EMM和LUOLUO的帮助,让我完成了这个小工具,发出来大家指教下,谢谢

dim siteindex
dim backupfile
set arg=wscript.Arguments
if arg.count<=0 then
wscript.echo "briis.vbs backup backupfile.txt"
wscript.echo "briis.vbs restore backupfile.txt"
wscript.echo "briis.vbs changeip"
wscript.quit
end 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 站点ID的索引
*********************************************
把站点的ID读入数组siteid方便下面修改
ChildObject.AdsPath IIS虚拟站点路径,对于第一个虚拟站点,这个值=IIS://localhost/w3svc/1,最后那个1是指虚拟站点的ID号。
Set w3svc = GetObject("IIS://localhost/w3svc") 建立IIS对象
For Each ChildObject In w3svc
If (Err.Number <> 0) Then Exit For
ChildObjectName = Replace(ChildObject.AdsPath, Left(ChildObject.AdsPath, 22), "") 过滤掉IIS虚拟路径前面的字符,把最后一位,也就是站点ID号取出来
If IsNumeric(ChildObjectName) = True Then 判断取出的ChildObjectName是不是数字,如果为真的话,就把值赋给siteid这个数组
Set iis = w3svc.GetObject("IIsWebServer", ChildObjectName)
If Err.Number <> 0 Then
Exit For
MsgBox ("出错")
wscript.quit
End If
ServerBindings = iis.ServerBindings 把IIS虚拟站点绑定的IP、端口、域名的值放入数组ServerBindings
ServerComment = iis.ServerComment 把IIS虚拟站点的名称赋值给变量ServerComment
MaxConnections = iis.MaxConnections 把IIS虚拟站点的最大连接数赋值给变量MaxConnections
set IIsWeb=iis.GetObject("IIsWebVirtualDir","Root")
Path=IIsWeb.Path
set fso=CreateObject("scripting.filesystemObject")
if (fso.fileexists(backupfile)) then
set flo=fso.OpenTextFile(backupfile,8,True)
flo.WriteLine(""&ServerComment&vbTab&join(ServerBindings,",")&vbTab&MaxConnections&vbTab&Path)
flo.close
wscript.echo "正在备份站点 "&ServerComment&"!"
else
set flo=fso.CreateTextFile(backupfile,true)
flo.WriteLine(""&ServerComment&vbTab&join(ServerBindings,",")&vbTab&MaxConnections&vbTab&Path)
flo.close
wscript.echo "正在备份站点 "&ServerComment&"!"
end if
siteindex = siteindex + 1
End If
Next
wscript.echo "一共备份了 "&siteindex-1&"个站点!"&vbcrlf
end function

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

Function ChangeSiteIP()
Dim siteid(1000),oldip,newip,w3svc,iis
*********************************************
获取两个IP,一个是原来的,是个是新的
oldip=inputbox("请输入原来的IP")
if oldip="" then
msgbox ("输入错误") 判断是否输入为空
wscript.quit
end if
newip=inputbox("请输入新的IP")
if isnull(newip) or newip="" then
msgbox ("输入错误") 判断是否输入为空
wscript.quit
end if
*********************************************

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

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

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) 然后创建一个WEB服务器

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

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

提交信息
ServerObj.SetInfo

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

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

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

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

 

补充:综合编程 , 安全编程 ,
Apache
IIS
Nginx
Tomcat
如果你遇到web 服务器难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,