WSH Practical Lecture---Lecture 2 Creating Users, Directories and Sites
Author:Eve Cole
Update Time:2009-05-30 19:53:49
Lecture 2 Creating users, directories and sites
-------------------------------------------------- ----------------------------------
This lecture will use ADSI, which is the Active Directory Service Interface. You can find some related information at 15Seconds.com.
1. Create a user. The following code creates user user1 on the independent server white. The initial password is user1, and ADSI is used.
Code:
-------------------------------------------------- ----------------------------------
Dim Username,UserPass Dim oDomain,oUser Username = "user1" UserPass = "user1" Set oDomain =
GetObject("WinNT://white") Set oUser = oDomain.Create ("user", UserName) If (err.number = 0) Then
oUser.SetInfo oUser.SetPassword UserPass oUser.SetInfo Else WScript.Echo "Create User" & UserName & "Error!" End If Set oUser = Nothing Set oDomain = Nothing
-------------------------------------------------- ----------------------------------
2. Create a directory Use FileSystemObject to create a directory:
Code:
-------------------------------------------------- ----------------------------------
Dim FsObject Dim tmpFolder Set FsObject = WScript.CreateObject("Scripting.FileSystemObject")
tmpFolder = "D:userdateuser1" If Not FsObject.FolderExists(tmpFolder) Then
FsObject.CreateFolder(tmpFolder) If Err.Number<>0 Then WScript.Echo "Create directory" & tmpFolder
& "Failure!" End If End If
-------------------------------------------------- ----------------------------------
Note that before creating the directory, check whether the directory exists. If it exists, there is no need to create it.
3. Create a site The following subroutine is responsible for creating a WWW site. The meaning of each parameter is: site IP address, site root directory, site description, host name, port number, computer name (changed to LOCALHOST), whether to start immediately, The account used for anonymous access, the password of the account used for anonymous access, and the directory of the LOG file.
The function returns the serial number of the created site in IIS (in IIS, all sites are numbered sequentially, with the first one being 1).
An example call:
Code:
-------------------------------------------------- ----------------------------------
siteid=
ASTCreateWebSite"10.1.3.122","d:userdatauser1","www_user1","","80","LocalHost",True,"IUSR_user1","8iui%
#","D:Logfiles")Function ASTCreateWebSite(IPAddress, RootDirectory, ServerComment, HostName, PortNum,
Computer, Start,AnonymousUserName,AnonymousUserPass,LogFileDirectory) Dim w3svc, WebServer,
NewWebServer, NewDir Dim Bindings, BindingString, NewBindings, Index, SiteObj, bDone On Error
Resume Next Err.Clear Set w3svc = GetObject("IIS://" & Computer & "/w3svc") If
Err.Number <> 0 Then WScript.Echo "Cannot open: "&"IIS://" & Computer & "/w3svc" & VbCrlf & "The program will exit." WScript.Quit (1) End If BindingString = IpAddress & ":" & PortNum & ":" &
HostName For Each WebServer in w3svc If WebServer.Class = "IIsWebServer" Then
Bindings = WebServer.ServerBindings If BindingString = Bindings(0) Then
WScript.Echo "IP address conflict:" & IpAddress & ",Please check the IP address!." & VbCrlf & "Cancel creation of this site."
Exit Function End If End If Next Index = 1 bDone = False
While (Not bDone) Err.Clear Set SiteObj = GetObject("IIS://"&Computer&"/w3svc/" &
Index) If (Err.Number = 0) Then Index = Index + 1 Else
Err.Clear Set NewWebServer = w3svc.Create("IIsWebServer", Index) If
(Err.Number <> 0) Then Index = Index + 1 Else Err.Clear
Set SiteObj = GetObject("IIS://"&Computer&"/w3svc/" & Index) If
(Err.Number = 0) Then bDone = True Else Index
= Index + 1 End If End If End If If (Index > 10000)
Then WScript.Echo "It seems that the site cannot be created. The serial number of the site being created is: "&Index&"." & VbCrlf & "Cancel the creation of this site." Exit Function End If Wend NewBindings = Array(0)
NewBindings(0) = BindingString NewWebServer.ServerBindings = NewBindings
NewWebServer.ServerComment = ServerComment NewWebServer.AnonymousUserName = AnonymousUserName
NewWebServer.AnonymousUserPass = AnonymousUserPass NewWebServer.KeyType = "IIsWebServer"
NewWebServer.FrontPageWeb = True NewWebServer.EnableDefaultDoc = True NewWebServer.DefaultDoc
= "Default.htm, Default.asp, Index.htm, Index.asp" NewWebServer.LogFileDirectory = LogFileDirectory
NewWebServer.SetInfo Set NewDir = NewWebServer.Create("IIsWebVirtualDir", "ROOT")
NewDir.Path = RootDirectory NewDir.AccessRead = true NewDir.AppFriendlyName = "Application" &
ServerComment NewDir.AppCreate True NewDir.AccessScript = True Err.Clear
NewDir.SetInfo If (Err.Number = 0) Then Else WScript.Echo "Error creating home directory."
End If If Start = True Then Err.Clear Set NewWebServer = GetObject("IIS://"
& Computer & "/w3svc/" & Index) NewWebServer.Start If Err.Number <> 0 Then
WScript.Echo "Error starting site!" Err.Clear Else End If End If
ASTCreateWebSite = IndexEnd Function The following function creates an FTP site: Function ASTCreateFtpSite(IPAddress,
RootDirectory, ServerComment, HostName, PortNum, Computer, Start,LogFileDirectory) Dim MSFTPSVC,
FtpServer, NewFtpServer, NewDir Dim Bindings, BindingString, NewBindings, Index, SiteObj, bDone
On Error Resume Next Err.Clear Set MSFTPSVC = GetObject("IIS://" & Computer & "/MSFTPSVC")
If Err.Number <> 0 Then WScript.Echo "Cannot open: "&"IIS://" & Computer & "/MSFTPSVC" & VbCrlf
& "The program will exit." WScript.Quit (1) End If BindingString = IpAddress & ":" & PortNum
& ":" & HostName For Each FtpServer in MSFTPSVC If FtpServer.Class="IIsFtpServer" Then
Bindings = FtpServer.ServerBindings If BindingString = Bindings(0) Then
WScript.Echo "IP address conflict:" & IpAddress & ",Please check the IP address!." & VbCrlf & "Cancel the creation of this site." Exit
Function End If End If Next Index = 1 bDone = False While
(Not bDone) Err.Clear Set SiteObj = GetObject("IIS://"&Computer&"/MSFTPSVC/" & Index)
If (Err.Number = 0) Then Index = Index + 1 Else Err.Clear
Set NewFtpServer = MSFTPSVC.Create("IIsFtpServer", Index) If (Err.Number <>
0) Then Index = Index + 1 Else Err.Clear
Set SiteObj = GetObject("IIS://"&Computer&"/MSFTPSVC/" & Index) If (Err.Number = 0)
Then bDone = True Else Index = Index + 1
End If End If End If If (Index > 10000) Then
WScript.Echo "It seems that the site cannot be created. The serial number of the site being created is: "&Index&"." & VbCrlf & "Cancel the creation of this site."
Exit Function End If Wend NewBindings = Array(0) NewBindings(0) =
BindingString NewFtpServer.ServerBindings = NewBindings NewFtpServer.ServerComment =
ServerComment NewFtpServer.AllowAnonymous = False NewFtpServer.AccessWrite = True
NewFtpServer.AccessRead = True NewFtpServer.DontLog = False NewFtpServer.LogFileDirectory =
LogFileDirectory NewFtpServer.SetInfo Set NewDir = NewFtpServer.Create
("IIsFtpVirtualDir", "ROOT") NewDir.Path = RootDirectory NewDir.AccessRead = true
Err.Clear NewDir.SetInfo If (Err.Number = 0) Then Else WScript.Echo "An error occurred while creating the home directory." End If If Start = True Then Err.Clear Set NewFtpServer =
GetObject("IIS://" & Computer & "/MSFTPSVC/" & Index) NewFtpServer.Start If
Err.Number <> 0 Then WScript.Echo "Error starting site!" Err.Clear Else
End If End If ASTCreateFtpSite = IndexEnd Function
-------------------------------------------------- ----------------------------------