WSH實用講座---第二講創建使用者、目錄和站點
作者:Eve Cole
更新時間:2009-05-30 19:53:49
第二講建立使用者、目錄和站點
-------------------------------------------------- ------------------------------
本講將使用到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:userdateuser1" 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: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 "無法開啟: "&"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
-------------------------------------------------- ------------------------------