使用ASP在IIS建立WEB站點的函數

程序代碼:html

'=========================================================='
函數介紹:建立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
web

'肯定一個不存在的站點編號作爲新建站點編號,系統默認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
api


使用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站點信息url

程序代碼:


'=====================================================
'函數介紹:列出當前服務器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 Programmaticallyhttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/iisref/html/psdk/asp/aint7e9l.asp

相關文章
相關標籤/搜索