利用VBS脚本自动创建计算机帐户的代码

所属分类: 脚本专栏 / vbs 阅读数: 2067
收藏 0 赞 0 分享
mcse注:其实这是 按照ADSI(Active Directory Services Interface:活动目录服务接口)写的程序。如果你安装了resource kit,这段代码可以用netcom这条命令进行工作,下面是netcom的一个例子: 

  NETDOM /Domain:MYDOMAIN /user:adminuser /password:apassword MEMBER MYCOMPUTER /ADD 

复制代码 代码如下:

  ***********************

  '* Start Script

  '***********************

  Dim sComputerName, sUserOrGroup, sPath, computerContainer, rootDSE, lFlag

  Dim secDescriptor, dACL, ACE, oComputer, sPwd

  '

  '* Declare constants used in defining the default location for the

  '* machine account, flags to identify the object as a machine account,

  '* and security flags

  'Const UF_WORKSTATION_TRUST_ACCOUNT = &H1000

  Const UF_ACCOUNTDISABLE = &H2

  Const UF_PASSWD_NOTREQD = &H20

  Const ADS_GUID_COMPUTRS_CONTAINER = "aa312825768811d1aded00c04fd8d5cd"

  Const ADS_ACETYPE_ACCESS_ALLOWED = 0

  Const ADS_ACEFLAG_INHERIT_ACE = 2

  '

  '* Set the flags on this object to identify it as a machine account

  '* and determine the name. The name is used statically here, but may

  '* be determined by a command line parameter or by using an InputBox

  'lFlag = UF_WORKSTATION_TRUST_ACCOUNT Or UF_ACCOUNTDISABLE Or UF_PASSWD_NOTREQD

  sComputerName = "TestAccount"

  '

  '* Establish a path to the container in the Active Directory where

  '* the machine account will be created. In this example, this will

  '* automatically locate a domain controller for the domain, read the

  '* domain name, and bind to the default "Computers" container

  '*********************************************************************

  Set rootDSE = GetObject("LDAP://RootDSE")

  sPath = "LDAP://  Set computerContainer = GetObject(sPath)

  sPath = "LDAP://" & computerContainer.Get("distinguishedName")

  Set computerContainer = GetObject(sPath)

  ''* Here, the computer account is created. Certain attributes must

  '* have a value before calling .SetInfo to commit (write) the object

  '* to the Active Directory

  'Set oComputer = computerContainer.Create("computer", "CN=" & sComputerName)

  oComputer.Put "samAccountName", sComputerName + "$"

  oComputer.Put "userAccountControl", lFlag

  oComputer.SetInfo

  '

  '* Establish a default password for the machine account

  'sPwd = sComputerName & "$"

  sPwd = LCase(sPwd)

  oComputer.SetPassword sPwd

  ''* Specify which user or group may activate/join this computer to the

  '* domain. In this example, "MYDOMAIN" is the domain name and

  '* "JoeSmith" is the account being given the permission. Note that

  '* this is the downlevel naming convention used in this example.

  'sUserOrGroup = "MYDOMAIN\joesmith"

  ''* Bind to the Discretionary ACL on the newly created computer account

  '* and create an Access Control Entry (ACE) that gives the specified

  '* user or group full control on the machine account

  'Set secDescriptor = oComputer.Get("ntSecurityDescriptor")

  Set dACL = secDescriptor.DiscretionaryAcl

  Set ACE = CreateObject("AccessControlEntry")

  '

  '* An AccessMask of "-1" grants Full Control

  '

  ACE.AccessMask = -1

  ACE.AceType = ADS_ACETYPE_ACCESS_ALLOWED

  ACE.AceFlags = ADS_ACEFLAG_INHERIT_ACE

  ''* Grant this control to the user or group specified earlier.

  'ACE.Trustee = sUserOrGroup

  '

  '* Now, add this ACE to the DACL on the machine account

  'dACL.AddAce ACE

  secDescriptor.DiscretionaryAcl = dACL

  '

  '* Commit (write) the security changes to the machine account

  'oComputer.Put "ntSecurityDescriptor", Array(secDescriptor)

  oComputer.SetInfo

  ''* Once all parameters and permissions have been set, enable the

  '* account.

  '

  oComputer.AccountDisabled = False

  oComputer.SetInfo

  ''* Create an Access Control Entry (ACE) that gives the specified user

  '* or group full control on the machine account

  'wscript.echo "The command completed successfully."

  '*****************

  '* End Script


更多精彩内容其他人还在看

用VBS实现的发送带Cookie的HTTP请求的代码

在昨天的《使用正确版本的XMLHTTP》中卖了个关子,ServerXMLHTTP的功能比XMLHTTP强大,你现在大概已经猜到了吧。没错,用ServerXMLHTTP可以在HTTP请求头中加入Cookie,而XMLHTTP不可以
收藏 0 赞 0 分享

校准系统时间的VBS代码

更新为自动判断时间格式,WIN7 XP测试通过,WIN8待测试,主要是通过获取百度的相关信息然后跟系统时间进行比较
收藏 0 赞 0 分享

VBS中获取系统本次及上次开关机时间的代码(WinXP/win2003/Win7兼容版)

获取系统本次及上次开关机时间的vbs代码,经测试兼容WinXP/win2003/Win7系统,喜欢的朋友可以收藏下
收藏 0 赞 0 分享

VBS合并一个文件夹里的TXT的实现代码

有时从网上的电子书网站上下载解压后。得到的是多个TXT文本。有没有办法把多个TXT合在一起看呢?写了个代码。不是很完美,欢迎高手批评指教。 我只是个菜鸟,需要大家的关怀才能进步哇
收藏 0 赞 0 分享

VBS 硬盘读写统计(分区读写统计)

用vbs实现的硬盘读写统计(分区读写统计)的实现代码,想学习vbs的朋友可以参考下
收藏 0 赞 0 分享

VBS实现工作表按指定表头自动分表

下面的VBS脚本就是实现的工作表按指定表头(由用户选择)自动分表功能。需要的朋友只要将要操作的工作表拖放到脚本文件上即可轻松实现工作表分表
收藏 0 赞 0 分享

VBS 修改远程桌面端口号的代码

仅有一个简单的功能——修改远程桌面端口。系统必须是XP。或许应该发到新手区
收藏 0 赞 0 分享

VBS 强制关闭Symantec Endpoint Protection的代码

很多企业电脑系统是Windows Xp,使用Windows server 2003 来控制,其中客户端得杀毒软件有不少是使用 Symantec Endpoint Protection
收藏 0 赞 0 分享

vbsTree VBS脚本模拟tree命令

用vbs输出一个文件夹的目录结构,喜欢的朋友可以测试下
收藏 0 赞 0 分享

Hardware_Info.vbs 获取硬件信息的VBS代码

代码没有技术水准,网上搜索有一大堆类似代码,关键是中文描述方式,支持多个硬盘、显卡、内存、声卡、网卡,CPU超频识别,支持cmd调用
收藏 0 赞 0 分享
查看更多