Program code:
'================================================== ========='
Function introduction: Create WebSite
'This function uses ADSI and requires Administrators group user rights.
'Function name: CreateWebSite(Computer,IPAddr,PortNum,HostName,WebSiteDirectory,LogDirectory,WebSiteInfo,
GuestUserName,GuestUserPass,StartOrStop)
'Usage: CreateWebSite computer name (move to LocalHost or 127.0.0.1), site IP address, port number, host name, site root directory, LOG file directory site description, account used when accessing the website, when accessing the website Password of the account used, whether to start the site
'Example: 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
'Check whether the W3SVC service (WEB service) can be loaded
Set w3svc = GetObject(IIS:// & Computer & /w3svc)
If Err.Number <> 0 Then 'Display error message
response.write cannot be opened: &IIS:// & Computer & /w3svc
response.end
End If
'Check whether there is a site with the same IP address, port and host name
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 address conflict: & IPAddr &, please check the IP address! .
Exit Function
End If
End If
Next
'Determine a non-existent site number as the new site number. The system default WebSite site number is 1, so it starts from 2
SiteNum=2
bDone = False
While (Not bDone)
Err.Clear
Set SiteObj = GetObject(IIS://&Computer&/w3svc/&SiteNum) 'Load the specified site
If (Err.Number = 0) Then
'response.write Step_1Site&SiteNum&exists
SiteNum = SiteNum + 1
Else
'response.write Step_1 site&SiteNum& does not exist
Err.Clear
Set NewWebServer = w3svc.Create(IIsWebServer,SiteNum) 'Create the specified site
If (Err.Number <> 0) Then
'response.write Step_2 site&SiteNum& failed to create
SiteNum = SiteNum + 1
Else
'response.write Step_2 Site&SiteNum& created successfully
bDone=True
End If
End If
If (SiteNum > 50) Then 'Maximum number of sites created by the server
response.write exceeds the maximum number of sites created by the server. The sequence number of the site being created is: &SiteNum&.
response.end
End If
Wend
'Perform basic site configuration
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 = Application&WebSiteInfo
NewDir.AppCreate True
NewDir.AccessScript = True
Err.Clear
NewDir.SetInfo
If (Err.Number <> 0) Then
response.write Error while creating home directory.
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 Error starting site!
response.end
Err.Clear
End If
End If
response.write The site is created successfully, the site number is: & SiteNum &, the domain name is: & HostName
End Function
Use ASP settings to specify the maximum CPU usage of the site
Program code:
'==================================================
'Function introduction: Set the maximum CPU usage of the specified site
'This function uses ADSI and requires Administrators group user rights.
'Function name: SetCPULimitVar (Computer,SiteNum,LimitVar)
'Usage: SetCPULimitVar computer name, site number, maximum limit value (100=1%, 1000=10%)
'Example: SetCPULimitVar LocalHost,2,2000
'==================================================
Function SetCPULimitVar(Computer,SiteNum,LimitVar)
Set MyObj001 = GetObject(IIS://&Computer&/W3SVC/&SiteNum)
'Set enable CPU limit
MyObj001.CpuLimitsEnabled = True
'Set limit usage quota value
MyObj001.CpuLimitLogEvent=LimitVar
MyObj001.setinfo
Set MyObj001=Nothing
End Function
Use ASP to start/stop specified WEB sites
Program code:
'================================================== ========
'Function introduction: Use ASP to start/stop the specified WEB site
'This function uses ADSI and requires Administrators group user rights.
'Function name: AdminWebSite(Computer,WebSiteNum,DoWhat)
'Usage: AdminWebSite(computer name, site number, start/stop)
'Example: Start the site with site number 1 on computer 127.0.0.1
'AdminWebSite 127.0.0.1,1,1
'Example: Stop the site with site number 1 on computer 127.0.0.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 & . Error code: & Hex(Err)& - & Unable to open the specified site<br>
End If
if Dowhat=1 then
'Use Start to start the site
objServer.Start
If Err.Number <> 0 Then
Response.Write cannot start the specified Web site<br>
else
Response.Write The specified Web site has been started<br>
End If
elseif DoWhat=0 then
'Use Stop to stop the site
objServer.Stop
If Err.Number <> 0 Then
Response.Write cannot stop the specified website<br>
else
Response.Write has stopped specifying the website<br>
End If
end if
End Function
Use ASP to list server WEB site information
Program code:
'================================================== ====
'Function introduction: List the current server WEB site information
'This function uses ADSI and requires Administrators group user rights.
'Function name: ListWebSite (Computer,Num)
'Usage: ListWebSite (computer name, display number of sites)
'Example: Display 1000 site information on computer 127.0.0.1
'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>The following displays information about all sites on the computer: &Computer&</b></p>
ShowWebSite = SiteObj.Get(ServerBindings) 'Get site IP address: port: host header
Info=split(ShowWebSite(0),:)
response.write site number: &i&<br>
response.write site IP address: &Info(0)&<br>
response.write site port: &Info(1)&<br>
response.write site host header: &Info(2)&<br><br>
end if
next
set SiteOjb=nothing
End Function
Use ASP to delete a specified IIS site
Program code:
'================================================== =====
'Function introduction: delete the specified IIS site
'This function uses ADSI and requires Administrators group user rights.
'Function name: DelWebSite (Computer,SiteNum)
'Usage: DelWebSite computer name, site number
'Example: DelWebSite 127.0.0.1,2
'================================================== =====
Function DelWebSite(Computer,SiteNum)
Set W3SVC = GetObject(IIS://&Computer&/w3svc)
W3SVC.delete IIsWebServer,SiteNum
response.write deleted successfully!
End Function
Manage NT accounts through ASP
Program code:
'================================================== ======
'Function introduction: manage NT accounts through ASP
'This function uses ADSI and requires Administrators group user rights.
'================================================== ===
'Add user
'Function usage: AddUser(Computer,UserName,PassWord,FullName,Info)
'Parameters: computer name, account name, account password, account full name, account description
'Example: AddUser 127.0.0.1, Test, Test, test administrator account, this account is added through ASP
'Modify the basic information of the specified user
'Function usage: EditUser(Computer,UserName,OldPassWord,PassWord,FullName,Info)
'Parameters: computer name, account name, account password, account full name, account description
'Example: EditUser 127.0.0.1, Test, Test2, test administrator account modification, this account has been modified through ASP
'Delete the specified user
'Function usage: DelUser(Computer,UserName)
'Parameters: computer name, user name'
'Example: DelUser 127.0.0.1,Test
================================================== ======
Function AddUser(Computer,UserName,PassWord,FullName,Info)
'Execute the create account command
Set ComputerObj = GetObject(WinNT://&Computer)
Set NewUser = ComputerObj.Create( User , UserName )
NewUser.SetInfo
'Configure account settings
NewUser.SetPassword ( PassWord ) 'Account password
NewUser.FullName = FullName 'Account full name
NewUser.Description = Info 'Account description
NewUser.UserFlags = &H10000 '&H20000 (The user must change the password the next time he logs in) &H0040 (The user is not allowed to change the password) &H10000 (The password is permanently correct) &H0002 (The account is temporarily suspended)
NewUser.SetInfo
response.write Account&UserName& was created successfully!
Set ComputerObj=nothing
End Function
Function EditUser(Computer,UserName,OldPassWord,PassWord,FullName,Info)
'Read user information
Set ChangeUserObj = GetObject(WinNT://&Computer&/&UserName&,User)
'Change account password
if PassWord<> then
ChangeUserObj.SetPassword PassWord
response.write Account password changed successfully! <br>
end if
'Modify account full name
if FullName<> then
UserFullName = ChangeUserObj.get(FullName)
ChangeUserObj.FullName = FullName
ChangeUserObj.SetInfo
response.write Account full name modified successfully! <br>
end if
'Modify account description
if Info<> then
UserFullName = ChangeUserObj.get(Description)
ChangeUserObj.Description = Info
ChangeUserObj.SetInfo
response.write Account description modified successfully! <br>
end if
Set ChangeUserObj=nothing
End Function
Function DelUser(Computer,UserName)
Set DelUserObj = GetObject(WinNT://&Computer&/&UserName)
If Err = &H800401E4 Then
Response.Write User&UserName& does not exist
Response.End
End If
Set DelObj = GetObject(DelUserObj.Parent)
DelObj.Delete User, DelUserObj.Name
Set DelUserObj = Nothing
Set DelObj = Nothing
Response.Write deleted successfully
End Function
Using ASP to control specified site parsing script language functions
Program code:
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
'Please use the same format of Aegis(Num)= to parse other files. Remember to define 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
'Show supported scripting language
response.write The current site supports parsing list:<br>
For ValueIndex = 0 To UBound(IIsWebServiceObj.ScriptMaps)
response.write IIsWebServiceObj.Get(ScriptMaps)(ValueIndex)
response.write <br>
Next
End Function
Use ASP to list NT user groups and users
Program code:
'================================================== ======
'Function introduction: List NT user groups and users
'This function uses ADSI and requires Administrators group user rights.
'Function name: ListGroup(Computer)
'Usage: ListGroup (computer name)
'Example: Display 127.0.0.1 computer NT user group and users
'ListGroup 127.0.0.1
'================================================== ======
Function ListGroup(Computer)
response.write <p><b>The following is the Computer&Computer& system user group and user list</b></p>
Set ComputerObj = GetObject(WinNT://&Computer)
ComputerObj.Filter = Array(Group)
For Each Member in ComputerObj
Response.Write User group: &Member.Name&<br>
ListUser Computer,Member.Name
Next
end Function
'List users of the specified user group
Function ListUser(Computer,Group)
Set UserObj = GetObject(WinNT://&Computer&/&Group)
For Each Member in UserObj.Members
Response.write Users in the group: &Member.Name &<br>
Next
End Function
IIS provides IIS Admin Objects, allowing users to manage IIS through programs. IIS Admin Objects are based on Microsoft Active Directory Service Interfaces (ADSI). Any programming language that supports Automation, such as VBScript/JScript in ASP, Visual Basic, Java, or C++ can use it.
You can refer to the following ASP sample program to modify the address of the IIS default site:
<%
Set IISOBJ = GetObject(IIS://MyServer/w3svc/1/root)
IISOBJ.Path = D:newroot
IISOBJ.SetInfo
Set IISOBJ=Nothing
%>
Note: The user must have sufficient permissions on IIS Metabase.
Regarding how to use programs to manage IIS, you can refer to the following articles on MSDN:
Administering IIS Programmatically
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iisref/html/psdk/asp/aint7e9l.asp