欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用ASP在IIS创建WEB站点的函数

程序员文章站 2022-05-18 12:52:04
程序代码: '=========================================================='函数介绍:创建website'本函数...

程序代码:

'=========================================================='
函数介绍:创建website
'本函数使用adsi,需要administrators组用户权限
'函数名称:createwebsite(computer,ipaddr,portnum,hostname,websitedirectory,logdirectory,websiteinfo,
guestusername,guestuserpass,startorstop)
'用法:createwebsite 计算机名(一搬为localhost或127.0.0.1),站点ip地址,端口号,主机名,站点根目录,,log文件的目录站点说明,网站访问时所使用的帐号,网站访问时所用帐号的口令,是否启动站点
'例:createwebsite "localhost","127.0.0.123","80","www.test.net","e:userdatausernum001",
"e:userdatausernum001logfiles","cnknow.com","iusr_num001_test.net",
"abc888",true
'==================================================
function createwebsite(computer,ipaddr,portnum,hostname,websitedirectory,logdirectory,websiteinfo,
guestusername,guestuserpass,startorstop)
dim w3svc, webserver, newwebserver, newdir
dim bindings, bindingstring, newbindings, sitenum, siteobj, bdone
on error resume next
err.clear
'检测是否能够加载w3svc服务(即web服务)
set w3svc = getobject("iis://" & computer & "/w3svc")
if err.number <> 0 then '显示错误提示
 response.write "无法打开: "&"iis://" & computer & "/w3svc"
 response.end
end if
'检测是否有设定相同ip地址、端口及主机名的站点存在
bindingstring = ipaddr & ":" & portnum & ":" & hostname
for each webserver in w3svc
 if webserver.class = "iiswebserver" then
 bindings = webserver.serverbindings
 if bindingstring = bindings(0) then
 response.write "ip地址冲突:" & ipaddr & ",请检测ip地址!."
 exit function
 end if
 end if
next

'确定一个不存在的站点编号做为新建站点编号,系统默认website站点编号为1,因此从2开始
sitenum=2
bdone = false
while (not bdone)
err.clear
set siteobj = getobject("iis://"&computer&"/w3svc/"&sitenum) '加载指定站点
if (err.number = 0) then
 'response.write " step_1站点"&sitenum&"存在"
 sitenum = sitenum + 1
else
 'response.write " step_1站点"&sitenum&"不存在"
 err.clear
 set newwebserver = w3svc.create("iiswebserver",sitenum) '创建指定站点
 if (err.number <> 0) then
 'response.write " step_2站点"&sitenum&"创建失败"
 sitenum = sitenum + 1
 else
 'response.write " step_2站点"&sitenum&"创建成功"
 bdone = true
 end if
end if
if (sitenum > 50) then '服务器最大创建站点数
 response.write "超出服务器最大创建站点数,正在创建的站点的序号为: "&sitenum&"."
 response.end
end if
wend

'进行站点基本配置
newbindings = array(0)
newbindings(0) = bindingstring
newwebserver.serverbindings = newbindings
newwebserver.servercomment= websiteinfo
newwebserver.anonymoususername= guestusername
newwebserver.anonymoususerpass= guestuserpass
newwebserver.keytype = "iiswebserver"
newwebserver.frontpageweb = true
newwebserver.enabledefaultdoc = true
newwebserver.defaultdoc = "default.htm, default.asp, index.htm, index.asp"
newwebserver.logfiledirectory= logdirectory
newwebserver.setinfo
set newdir = newwebserver.create("iiswebvirtualdir", "root")
newdir.path = websitedirectory
newdir.accessread = true
newdir.appfriendlyname = "应用程序" & websiteinfo
newdir.appcreate true
newdir.accessscript = true
err.clear
newdir.setinfo
if (err.number <> 0) then
 response.write "主目录创建时出错."
 response.end
end if
if startorstop = true then
 err.clear
 set newwebserver = getobject("iis://" & computer & "/w3svc/" & sitenum)
 newwebserver.start
 if err.number <> 0 then
 response.write "启动站点时出错!"
 response.end
 err.clear
 end if
end if
response.write "站点创建成功,站点编号为:"& sitenum &" ,域名为:"& hostname
end function


使用asp设置指定站点cpu最大使用程度
程序代码:


'=================================================
'函数介绍:设置指定站点cpu最大使用程度
'本函数使用adsi,需要administrators组用户权限
'函数名称:setcpulimitvar (computer,sitenum,limitvar)
'用法:setcpulimitvar 计算机名,站点编号,最大限制值(100=1%,1000=10%)
'例:setcpulimitvar "localhost","2","2000"
'=================================================
function setcpulimitvar(computer,sitenum,limitvar)
set myobj001 = getobject("iis://"&computer&"/w3svc/"&sitenum)
'设置启用cpu限制
myobj001.cpulimitsenabled = true
'设置限制使用额度值
myobj001.cpulimitlogevent=limitvar
myobj001.setinfo
set myobj001=nothing
end function

使用asp启动/停止指定web站点

程序代码:


'=========================================================
'函数介绍:使用asp启动/停止指定web站点
'本函数使用adsi,需要administrators组用户权限
'函数名称:adminwebsite(computer,websitenum,dowhat)
'用法:adminwebsite(计算机名称,站点编号,启动/停止)
'例:启动127.0.0.1计算机上站点编号为1的站点
'adminwebsite "127.0.0.1","1",1
'例:停止127.0.0.1计算机上站点编号为1的站点
'adminwebsite "127.0.0.1","1",0
'=======================================================
function adminwebsite(computer,websitenum,dowhat)
on error resume next
set objserver = getobject("iis://" & computer & "/w3svc/" & websitenum)
if err.number <> 0 then
 response.write now & ". 错误码: " & hex(err)& " - " & "无法开启指定站点<br>"
end if
if dowhat=1 then
'使用start启动站点
 objserver.start
 if err.number <> 0 then
 response.write "无法启动指定web站点<br>"
 else
 response.write "已经启动指定web站点<br>"
 end if
elseif dowhat=0 then
 '使用stop停止站点
 objserver.stop
 if err.number <> 0 then
 response.write "无法停止指定web站点<br>"
 else
 response.write "已经停止指定web站点<br>"
 end if
end if
end function


使用asp列出服务器web站点信息

程序代码:


'=====================================================
'函数介绍:列出当前服务器web站点信息
'本函数使用adsi,需要administrators组用户权限
'函数名称:listwebsite (computer,num)
'用法:listwebsite (计算机名称,显示站点数量)
'例:显示127.0.0.1计算机上1000个站点信息
'listwebsite "127.0.0.1","1000"
'====================================================
function listwebsite(computer,num)
on error resume next
set siteobj = getobject("iis://"&computer&"/w3svc/"&i)
for i=0 to num
 err.clear
 if err.number=0 then
 response.write "<p><b>以下显示为计算机:"&computer&"上所有站点信息</b></p>"
 showwebsite = siteobj.get("serverbindings") '获得站点ip地址:端口:主机头
 info=split(showwebsite(0),":")
 response.write "站点编号:"&i&"<br>"
 response.write "站点ip地址:"&info(0)&"<br>"
 response.write "站点端口:"&info(1)&"<br>"
 response.write "站点主机头:"&info(2)&"<br><br>"
 end if
next
set siteojb=nothing
end function

使用asp删除指定iis站点

程序代码:


'======================================================
'函数介绍:删除指定iis站点
'本函数使用adsi,需要administrators组用户权限
'函数名称:delwebsite (computer,sitenum)
'用法:delwebsite 计算机名,站点编号
'例:delwebsite "127.0.0.1","2"
'======================================================
function delwebsite(computer,sitenum)
set w3svc = getobject("iis://"&computer&"/w3svc")
w3svc.delete "iiswebserver",sitenum
response.write "删除成功!"
end function


通过asp管理nt帐号

程序代码:


'=======================================================
'函数介绍:通过asp管理nt帐号
'本函数使用adsi,需要administrators组用户权限
'====================================================
'填加用户
'函数用法:adduser(computer,username,password,fullname,info)
'参数:计算机名,帐号名称,帐号密码,帐号全名,帐号说明
'例:adduser "127.0.0.1","test","test","测试管理员帐号","本帐号通过asp填加"
'修改指定用户基本信息
'函数用法:edituser(computer,username,oldpassword,password,fullname,info)
'参数:计算机名,帐号名称,帐号密码,帐号全名,帐号说明
'例:edituser "127.0.0.1","test","test2","测试管理员帐号修改","本帐号通过asp修改过"
'删除指定用户
'函数用法:deluser(computer,username)
'参数:计算机名,用户名'
'例:deluser "127.0.0.1","test"
========================================================
function adduser(computer,username,password,fullname,info)
'执行创建帐号命令
set computerobj = getobject("winnt://"&computer)
set newuser = computerobj.create( "user" , username )
newuser.setinfo
'进行帐号设置
newuser.setpassword ( password ) '帐号密码
newuser.fullname = fullname '帐号全名
newuser.description = info '帐号说明
newuser.userflags = &h10000 '&h20000(使用者下次登入时须变更密码) &h0040(使用者不得变更密码) &h10000(密码永久正确) &h0002(帐户暂时停用)
newuser.setinfo
response.write "帐号"&username&"创建成功!"
set computerobj=nothing
end function

function edituser(computer,username,oldpassword,password,fullname,info)
'读取用户信息
set changeuserobj = getobject("winnt://"&computer&"/"&username&",user")
'修改帐号密码
if password<>"" then
 changeuserobj.setpassword password
 response.write "帐号密码修改成功!<br>"
end if
'修改帐号全名
if fullname<>"" then
 userfullname = changeuserobj.get("fullname")
 changeuserobj.fullname = fullname
 changeuserobj.setinfo
 response.write "帐号全名修改成功!<br>"
end if
'修改帐号说明
if info<>"" then
 userfullname = changeuserobj.get("description")
 changeuserobj.description = info
 changeuserobj.setinfo
 response.write "帐号说明修改成功!<br>"
end if
set changeuserobj=nothing
end function

function deluser(computer,username)
set deluserobj = getobject("winnt://"&computer&"/"&username)
if err = &h800401e4 then
 response.write "用户"&username&"不存在"
 response.end
end if
set delobj = getobject(deluserobj.parent)
delobj.delete "user", deluserobj.name
set deluserobj = nothing
set delobj = nothing
response.write "删除成功"
end function


使用asp控制指定站点解析脚本语言函数

程序代码:


function adminaegis(computer,sitenum)
set iiswebserviceobj = getobject("iis://"&computer&"/w3svc/"&sitenum)
dim aegis(1)
aegis(0)=".asp,c:winntsystem32inetsrvasp.dll,5,get,head,post,trace"
'aegis(1)=".aspx,c:winntmicrosoft.netframeworkv1.1.4322aspnet_isapi.dll,1,get,head,
post,debug"
'解析其它文件请同样使用aegis(num)=""这种格式,记得要将dim aegis(num)定义
'.htw,c:winntsystem32webhits.dll,3,get,head,post
'.ida,c:winntsystem32idq.dll,7,get,head,post
'.idq,c:winntsystem32idq.dll,7,get,head,post
'.asp,c:winntsystem32inetsrvasp.dll,5,get,head,post,trace
'.cer,c:winntsystem32inetsrvasp.dll,5,get,head,post,trace
'.cdx,c:winntsystem32inetsrvasp.dll,5,get,head,post,trace
'.asa,c:winntsystem32inetsrvasp.dll,5,get,head,post,trace
'.idc,c:winntsystem32inetsrvhttpodbc.dll,5,get,post<br>
'.shtm c:winntsystem32inetsrvssinc.dll,5,get,post<br>
'.shtml c:winntsystem32inetsrvssinc.dll,5,get,post<br>
'.stm c:winntsystem32inetsrvssinc.dll,5,get,post<br>
'.asax c:winntmicrosoft.netframeworkv1.1.4322aspnet_isapi.dll,5,get,head,post,
debug
'.ascx,c:winntmicrosoft.netframeworkv1.1.4322aspnet_isapi.dll,5,get,head,
post,debug
'.ashx,c:winntmicrosoft.netframeworkv1.1.4322aspnet_isapi.dll,1,get,head,
post,debug
'.asmx,c:winntmicrosoft.netframeworkv1.1.4322aspnet_isapi.dll,1,get,head,
post,debug
'.aspx,c:winntmicrosoft.netframeworkv1.1.4322aspnet_isapi.dll,1,get,head,
post,debug
'.axd,c:winntmicrosoft.netframeworkv1.1.4322aspnet_isapi.dll,1,get,head,
post,debug
'.vsdisco,c:winntmicrosoft.netframeworkv1.1.4322aspnet_isapi.dll,1,get,head,
post,debug
'.rem,c:winntmicrosoft.netframeworkv1.1.4322aspnet_isapi.dll,1,get,head,
post,debug
'.soap,c:winntmicrosoft.netframeworkv1.1.4322aspnet_isapi.dll,1,get,head,
post,debug
'.config,c:winntmicrosoft.netframeworkv1.1.4322aspnet_isapi.dll,5,get,head,
post,debug
'.cs,c:winntmicrosoft.netframeworkv1.1.4322aspnet_isapi.dll,5,get,head,
post,debug
'.csproj,c:winntmicrosoft.netframeworkv1.1.4322aspnet_isapi.dll,5,get,head,
post,debug
'.vb,c:winntmicrosoft.netframeworkv1.1.4322aspnet_isapi.dll,5,get,head,
post,debug
'.vbproj,c:winntmicrosoft.netframeworkv1.1.4322aspnet_isapi.dll,5,get,head,
post,debug
'.webinfo,c:winntmicrosoft.netframeworkv1.1.4322aspnet_isapi.dll,5,get,head,
post,debug
'.licx,c:winntmicrosoft.netframeworkv1.1.4322aspnet_isapi.dll,5,get,head,
post,debug
'.resx,c:winntmicrosoft.netframeworkv1.1.4322aspnet_isapi.dll,5,get,head,
post,debug
'.resources,c:winntmicrosoft.netframeworkv1.1.4322aspnet_isapi.dll,5,get,
head,post,debug
iiswebserviceobj.scriptmaps=aegis
iiswebserviceobj.setinfo
'显示支持脚本语言
response.write "当前站点支持解析列表:<br>"
for valueindex = 0 to ubound(iiswebserviceobj.scriptmaps)
 response.write iiswebserviceobj.get("scriptmaps")(valueindex)
 response.write "<br>"
next
end function


使用asp列出nt用户组及用户

程序代码:


'=======================================================
'函数介绍:列出nt用户组及用户
'本函数使用adsi,需要administrators组用户权限
'函数名称:listgroup(computer)
'用法:listgroup(计算机名称)
'例:显示127.0.0.1计算机nt用户组及用户
'listgroup "127.0.0.1"
'=======================================================
function listgroup(computer)
response.write "<p><b>以下为计算机"&computer&"系统用户组及用户列表</b></p>"
set computerobj = getobject("winnt://"&computer)
computerobj.filter = array("group")
for each member in computerobj
 response.write "用户组:"&member.name&"<br>"
 listuser computer,member.name
next
end function

'列出指定用户组用户
function listuser(computer,group)
set userobj = getobject("winnt://"&computer&"/"&group)
for each member in userobj.members
 response.write "    组中用户:"&member.name &"<br>"
next
end function

iis 提供了 iis admin objects,让用户可以通过程序来管理iis。iis admin objects 基于 microsoft active directory service interfaces (adsi)。任何支持 automation 的编程语言,如 asp 中的 vbscript/jscript,visual basic, java, 或 c++ 都能使用它。
您可以参考以下的 asp 示例程序来修改 iis 默认站点的地址:
<%
       set iisobj = getobject("iis://myserver/w3svc/1/root")
       iisobj.path = "d:newroot"
       iisobj.setinfo
       set iisobj=nothing
%>
注意:用户必须要对 iis metabase 有足够的权限。
关于如何用程序来管理 iis,您可以到 msdn 参考以下的文章:
administering iis programmatically
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iisref/html/psdk/asp/aint7e9l.asp
阅读全文(55) | 回复(0) | 引用通告(0) | 编辑