第二講 創建用戶、目錄和站點
--------------------------------------------------------------------------------
本講將使用到ADSI,即活動目錄服務接口.可以到15Seconds.com找到一些相關的資料.
1.創建用戶
下面這段代碼在獨立服務器white上創建用戶user1,初始口令user1,用到了ADSI.
代碼:
--------------------------------------------------------------------------------
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 "創建用
戶" & UserName & "出錯!" End If Set oUser = Nothing Set oDomain = Nothing
--------------------------------------------------------------------------------
2.創建目錄
使用FileSystemObject創建目錄:
代碼:
--------------------------------------------------------------------------------
Dim fsobject Dim tmpFolder Set FsObject = WScript.CreateObject("Scripting.FileSystemObject")
tmpFolder = "D:\userdate\user1" If Not FsObject.FolderExists(tmpFolder) Then
FsObject.CreateFolder(tmpFolder) If Err.Number<>0 Then WScript.Echo "創建目錄" & tmpFolder
& "失敗!" End If End If
--------------------------------------------------------------------------------
注意在創建目錄前,先檢查了目錄是否存在,如果存在,則不用創建了.
3.創建站點
下面這個子程序負責創建一個WWW站點,各個參數的意義為:站點IP地址,站點根目錄,站點說明,主機名,端口號,計算機名(一搬
為LOCALHOST),是否立即啟動,匿名訪問時所使用的帳號,匿名訪問時所用帳號的口令,LOG文件的目錄.
函數返回所建站點在IIS中的序號(在IIS中,所有站點依次編號,第一個為1).
一個調用示例:
代碼:
--------------------------------------------------------------------------------
siteid =
ASTCreateWebSite"10.1.3.122","d:\userdata\user1","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 "無法打開: "&"IIS://" & Computer & "/w3svc" & VbCrlf & "程序將退
出." 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地址沖突:" & IpAddress & ",請檢測IP地址!." & VbCrlf & "取消創建本站點。"
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 "看起來不能創建站點,正在創建的站點的序號為: "&Index&"." & VbCrlf & "取消創建
本站點。" 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 = "應用程序" &
ServerComment NewDir.AppCreate True NewDir.AccessScript = True Err.Clear
NewDir.SetInfo If (Err.Number = 0) Then Else WScript.Echo "主目錄創建時出錯."
End If If Start = True Then Err.Clear Set NewWebServer = GetObject("IIS://"
& Computer & "/w3svc/" & Index) NewWebServer.Start If Err.Number <> 0 Then
WScript.Echo "啟動站點時出錯!" Err.Clear Else End If End If
ASTCreateWebSite = IndexEnd Function 下面函數創建FTP站點: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 "無法打開: "&"IIS://" & Computer & "/MSFTPSVC" & VbCrlf
& "程序將退出." 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地址沖突:" & IpAddress & ",請檢測IP地址!." & VbCrlf & "取消創建本站點。" 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 "看起來不能創建站點,正在創建的站點的序號為: "&Index&"." & VbCrlf & "取消創建本站點。"
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 "主目錄創
建時出錯." End If If Start = True Then Err.Clear Set NewFTPServer =
GetObject("IIS://" & Computer & "/MSFTPSVC/" & Index) NewFTPServer.Start If
Err.Number <> 0 Then WScript.Echo "啟動站點時出錯!" Err.Clear Else
End If End If ASTCreateFTPSite = IndexEnd Function