WSH实用讲座---第二讲 创建用户、目录和站点
浏览:28日期:2022-11-27
第二讲 创建用户、目录和站点--------------------------------------------------------------------------------本讲将使用到ADSI,即活动目录服务接口.可以到15Seconds.com找到一些相关的资料. 1.创建用户下面这段代码在独立服务器white上创建用户user1,初始口令user1,用到了ADSI.代码:--------------------------------------------------------------------------------Dim Username,UserPassDim oDomain,oUserUsername = "user1"UserPass = "user1"Set oDomain = GetObject("WinNT://white")Set oUser = oDomain.Create ("user", UserName)If (err.number = 0) ThenoUser.SetInfooUser.SetPassword UserPassoUser.SetInfoElseWScript.Echo "创建用户" & UserName & "出错!"End IfSet oUser = NothingSet oDomain = Nothing-------------------------------------------------------------------------------- 2.创建目录使用FileSystemObject创建目录:代码:--------------------------------------------------------------------------------Dim FsObjectDim tmpFolderSet FsObject = WScript.CreateObject("Scripting.FileSystemObject")tmpFolder = "D:userdateuser1"If Not FsObject.FolderExists(tmpFolder) ThenFsObject.CreateFolder(tmpFolder)If Err.Number<>0 ThenWScript.Echo "创建目录" & tmpFolder & "失败!"End IfEnd 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, NewDirDim Bindings, BindingString, NewBindings, Index, SiteObj, bDoneOn Error Resume NextErr.ClearSet w3svc = GetObject("IIS://" & Computer & "/w3svc")If Err.Number <> 0 ThenWScript.Echo "无法打开: "&"IIS://" & Computer & "/w3svc" & VbCrlf & "程序将退出."WScript.Quit (1)End IfBindingString = IpAddress & ":" & PortNum & ":" & HostNameFor Each WebServer in w3svcIf WebServer.Class = "IIsWebServer" Then Bindings = WebServer.ServerBindingsIf BindingString = Bindings(0) ThenWScript.Echo "IP地址冲突:" & IpAddress & ",请检测IP地址!." & VbCrlf & "取消创建本站点。"Exit FunctionEnd IfEnd IfNextIndex = 1bDone = FalseWhile (Not bDone)Err.ClearSet SiteObj = GetObject("IIS://"&Computer&"/w3svc/" & Index)If (Err.Number = 0) ThenIndex = Index + 1ElseErr.ClearSet NewWebServer = w3svc.Create("IIsWebServer", Index)If (Err.Number <> 0) ThenIndex = Index + 1ElseErr.Clear Set SiteObj = GetObject("IIS://"&Computer&"/w3svc/" & Index)If (Err.Number = 0) ThenbDone = TrueElseIndex = Index + 1End IfEnd IfEnd IfIf (Index > 10000) ThenWScript.Echo "看起来不能创建站点,正在创建的站点的序号为: "&Index&"." & VbCrlf & "取消创建本站点。"Exit FunctionEnd IfWendNewBindings = Array(0)NewBindings(0) = BindingStringNewWebServer.ServerBindings = NewBindingsNewWebServer.ServerComment = ServerCommentNewWebServer.AnonymousUserName = AnonymousUserNameNewWebServer.AnonymousUserPass = AnonymousUserPassNewWebServer.KeyType = "IIsWebServer"NewWebServer.FrontPageWeb = TrueNewWebServer.EnableDefaultDoc = TrueNewWebServer.DefaultDoc = "Default.htm, Default.asp, Index.htm, Index.asp"NewWebServer.LogFileDirectory = LogFileDirectoryNewWebServer.SetInfoSet NewDir = NewWebServer.Create("IIsWebVirtualDir", "ROOT")NewDir.Path = RootDirectoryNewDir.AccessRead = trueNewDir.AppFriendlyName = "应用程序" & ServerCommentNewDir.AppCreate TrueNewDir.AccessScript = TrueErr.ClearNewDir.SetInfoIf (Err.Number = 0) ThenElseWScript.Echo "主目录创建时出错."End IfIf Start = True ThenErr.ClearSet NewWebServer = GetObject("IIS://" & Computer & "/w3svc/" & Index)NewWebServer.StartIf Err.Number <> 0 ThenWScript.Echo "启动站点时出错!"Err.ClearElseEnd IfEnd IfASTCreateWebSite = IndexEnd Function下面函数创建FTP站点:Function ASTCreateFtpSite(IPAddress, RootDirectory, ServerComment, HostName, PortNum, Computer, Start,LogFileDirectory)Dim MSFTPSVC, FtpServer, NewFtpServer, NewDirDim Bindings, BindingString, NewBindings, Index, SiteObj, bDoneOn Error Resume NextErr.ClearSet MSFTPSVC = GetObject("IIS://" & Computer & "/MSFTPSVC")If Err.Number <> 0 ThenWScript.Echo "无法打开: "&"IIS://" & Computer & "/MSFTPSVC" & VbCrlf & "程序将退出."WScript.Quit (1)End IfBindingString = IpAddress & ":" & PortNum & ":" & HostNameFor Each FtpServer in MSFTPSVCIf FtpServer.Class="IIsFtpServer" ThenBindings = FtpServer.ServerBindingsIf BindingString = Bindings(0) ThenWScript.Echo "IP地址冲突:" & IpAddress & ",请检测IP地址!." & VbCrlf & "取消创建本站点。"Exit FunctionEnd IfEnd IfNextIndex = 1bDone = FalseWhile (Not bDone)Err.ClearSet SiteObj = GetObject("IIS://"&Computer&"/MSFTPSVC/" & Index)If (Err.Number = 0) ThenIndex = Index + 1ElseErr.ClearSet NewFtpServer = MSFTPSVC.Create("IIsFtpServer", Index)If (Err.Number <> 0) ThenIndex = Index + 1ElseErr.Clear Set SiteObj = GetObject("IIS://"&Computer&"/MSFTPSVC/" & Index)If (Err.Number = 0) ThenbDone = TrueElseIndex = Index + 1 End IfEnd IfEnd IfIf (Index > 10000) ThenWScript.Echo "看起来不能创建站点,正在创建的站点的序号为: "&Index&"." & VbCrlf & "取消创建本站点。"Exit FunctionEnd IfWendNewBindings = Array(0)NewBindings(0) = BindingStringNewFtpServer.ServerBindings = NewBindingsNewFtpServer.ServerComment = ServerCommentNewFtpServer.AllowAnonymous = FalseNewFtpServer.AccessWrite = TrueNewFtpServer.AccessRead = TrueNewFtpServer.DontLog = FalseNewFtpServer.LogFileDirectory = LogFileDirectoryNewFtpServer.SetInfoSet NewDir = NewFtpServer.Create("IIsFtpVirtualDir", "ROOT")NewDir.Path = RootDirectoryNewDir.AccessRead = trueErr.ClearNewDir.SetInfoIf (Err.Number = 0) ThenElseWScript.Echo "主目录创建时出错."End IfIf Start = True ThenErr.ClearSet NewFtpServer = GetObject("IIS://" & Computer & "/MSFTPSVC/" & Index)NewFtpServer.StartIf Err.Number <> 0 ThenWScript.Echo "启动站点时出错!"Err.ClearElseEnd IfEnd IfASTCreateFtpSite = IndexEnd Function--------------------------------------------------------------------------------
上一条:WSH简介下一条:WSH实用讲座:第一讲 获取机器的网络属性配置
相关文章: