当前位置:编程学习 > VB >>

VBS脚本添加系统帐户及从新启动和关闭计算机的办法

作者:黑妖蛇
注:其实这是 按照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://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 = "MYDOMAINjoesmith"

*********************************************************************
* 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
*****************
出于很多管理限制,可能无法直接使用Windows的explorer shell,需要在命令行方式下对远程计算机进

行操作。而命令行下所支持的命令远不能满足我们的需要,除了Windows XP支持用shutdown命令重启、关

闭或注销当前计算机之外,其他的Windows平台均未将类似功能作为基本命令。“Windows 管理规范”

(WMI) 脚本系统(如 Windows 主机脚本)的 WMI 可以用来检索大多数计算机系统方面的配置详细信息(

包括服务器应用),或者做出可以影响到系统的更改。其实WMI我们并不陌生,有几个管理工具就是启用

WMI的,比如“系统属性”、“系统信息”和“服务”的“依存关系”组件。

1.重启远程或者本地计算机

打开任意文本编辑器,(建议用UltraEdit,因为可以自动设置内容的醒目提示类型,关键字会加亮

显示)键入以下内容:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,(Shutdown)}!\" & strComputer & "ootcimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
ObjOperatingSystem.Reboot()
Next
另存为.VBS文件,比如reboot.vbs,放入systemrootsystem32下(systemroot是指Windows的安装目录)

,在命令行输入reboot就会自动执行该脚本。之所以放在systemroot下是因为Windows默认设置的系统环

境变量在此文件夹。需要说明的是strComputer这个变量:如果这个脚本要在远程计算机上执行,则变量

内容"."修改成远程计算机名。例如我们要在LAN内机器名为Server的计算机上执行这个脚本,那么变量名

相应修改成strComputer = "Server"。前提是已经取得Server上的相应权限。

2.关闭本地计算机

XP的shutdown命令需要设置一些参数才能正确执行,远不如在命令行下执行VBS脚本方便。同样在文

本编辑器中键入以下内容:
Set colOperatingSystems = GetObject("winmgmts:{(Shutdown)}").ExecQuery("Select * from

Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
ObjOperatingSystem.Win32Shutdown(1)
Next
另存为.VBS文件,如果在XP下就不要存成和shutdown同名了。执行后能够快速关闭计算机。

提示:关于Windows脚本宿主的升级。如果当前使用的是 Windows 2000 或 Windows ME,则计算机系

统中安装的是 2.0 版的 WSH。如果运行的是 Windows 95、98 或 Windows NT 4.0,则安装的是 1.0 版

。若要升级到 WSH 5.6,请访问 Microsoft Windows 脚本技术 Web 站点,网址为:

(http://www.microsoft.com/china/scripting)。升级到最新版本以保证脚本能够正确执行。

补充:软件开发 , Vb ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,