Windows脚本编程核心技术精解Chapter24.pdf
《Windows脚本编程核心技术精解Chapter24.pdf》由会员分享,可在线阅读,更多相关《Windows脚本编程核心技术精解Chapter24.pdf(28页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Chapter 24Managing Windows NT/2000 ServerIn This Chapter?Manage Windows NT through the APIadd/delete users,changepermissions,and change group membership?Find out about ADSI and how you get it?Discover the ADSI object model,and change user account properties?Manage services:start,stop,and pause them,
2、even on remote systems?Shut down your local machine or any remote computer?Add secret Registry keys to automatically log onto Windows NTScripts are perfect for administering Windows NT/2000.Through API callsand the new ADSI(Active Directory Service Interface),your scripts canmanage user accounts,fil
3、e shares,printers,and services.In conjunction withthe file system methods,you can easily transfer hundreds of user profiles ina matter of minutes.In this chapter,you learn how to use both the API andADSI to manage all kinds of server-related tasks.Managing Windows NT/2000 SecurityManaging Windows NT
4、/2000 can be annoying at timesits just too muchwork to administer hundreds of user accounts manually.Isnt there somemacro language that can automatically take care of routine work?There is.VBScript and the Windows Scripting Host can help you administerWindows NT/2000,and Microsoft has made VBScript
5、the official newautomation language of Windows 2000.This shows you the new emphasisMicrosoft puts on VBScript.4684-8 ch24.f.qc 3/3/00 9:45 AM Page 669However,VBScript itself is only the framework.It doesnt provide anymethods to access user accounts and change security settings.There aretwo ways to a
6、dd these capabilities:IWrite your own COM objects.Youve seen many COM objects throughoutthis book serve as scripting extensions,and Windows NT managementfunctions provided by the API can easily“wrap”inside COM objects.IAdd ADSI to your system.ADSI is the new management standardintroduced with Window
7、s 2000.Its a general interface to standardizehow scripts talk to different namespaces.You dont need Windows 2000to take advantage of ADSI,though.ADSI is available separately,and itsfree.All you need is to download the ADSI update.The big ADSI advantage is its availability:You can find it on any Wind
8、ows2000 machine,and you can add it to other Windows versions.Also,ADSI is ageneral interface,and you can access Exchange mail accounts or the ActiveDirectory.However,its drawback is speed:ADSI is much slower than directAPI calls.Especially if you plan to batch-update large numbers of accounts,you sh
9、ould use the API approach.Creating scripting extensions to manageuser accountsTable 24-1 lists some scripting extensions Ive created to help you manageWindows NT/2000.However,these extensions are provided as sample only.Ina professional production environment,review the source code to make sureit wo
10、rks as intended.Be extremely cautious if you decide to experiment withthese extensions.Ive provided full source code so you can understand and expand the COMobjects as you like.Make sure you have installed the COM objects listsetin Table 24-1.They are needed for the API-based scripts.Table 24-1COM O
11、bjects Provided With This BookCOM ObjectDescriptioninstallntusersetup.exeManage user accountsinstallntservicesetup.exeManage system servicesinstallntshutdownsetup.exeShutdown a system locally or remotelyGetting ready for ADSIADSI is exciting and much more suitable for scripting.It will be the newsta
12、ndard once Windows 2000 takes over,so its a good idea to get670Part V:Accessing Hidden ComponentsII4684-8 ch24.f.qc 3/3/00 9:45 AM Page 670accustomed with it.ADSI is free,and you can upgrade Windows NT 4 bygoing to for the suitable update for your system and download the package.Itscomparably lightw
13、eight.Once you have updated your system to ADSI,youcan immediately take advantage of all the ADSI sample scripts.Managing User Accounts(the API Way)Fiddling around manually with user accounts is very time-consuming anderror-prone,at least in larger companies.Dont do it.You can forget mostof the dial
14、og boxes with your new scripting extensions and manage useraccounts solely by script once you have installed the COM script extensionas outlined previously.Enumerating usersTo find out which users are defined on a specific computer,use EnumUsers:24-1.VBSset tool=CreateObject(“nt.user”)MsgBox tool.En
15、umUsersMsgBox tool.EnumUsers(“scenic”)You can query local users as well as user accounts on remote machines aslong as you have the necessary permissions.EnumUsersreturns the information as name,comment,usercomment,andfull name.Use Splitto get to the individual information:24-2.VBSset tool=CreateObje
16、ct(“nt.user”)users=Split(tool.EnumUsers,vbCr)for x=0 to UBound(users)-1infos=Split(users(x),vbTab)list=list&“Username:“&infos(0)&vbCrlist=list&“Comment:“&infos(1)&vbCr&vbCrnextMsgBox list,vbInformationChapter 24:Managing Windows NT/2000 Server671II4684-8 ch24.f.qc 3/3/00 9:45 AM Page 671EnumUserssup
17、ports a second parameter that works as a filter:Table 24-2Filter Codes for Enumerating User AccountsFilterDescription1Local user account data on a domain controller2Global user account data on a computer4Combination of all8Domain trust account data on a domain controller16Workstation or member serve
18、r account data on a domain controller32Domain controller account data on a domain controllerThe next script shows the differences.You can combine filter flags to includemore than one group:24-3.VBSset tool=CreateObject(“nt.user”)for i=0 to 5users=Split(tool.EnumUsers(,2i),vbCrLf)list=“Enumerating us
19、ing filter“&2i&vbCrfor x=0 to UBound(users)-1infos=Split(users(x),vbTab)list=list&“Username:“&infos(0)&vbCrnextMsgBox list,vbInformationnextAdding usersYour scripts can add users,too.Use AddUser:24-4.VBSset tool=CreateObject(“nt.user”)if tool.AddUser(“”,“testaccount”,“secret”,_5,“c:userstest”,“this
20、is a test account”)thenMsgBox“User account added!”elseMsgBox“Couldnt add user account:“&tool.GetLastErrorend ifThis is the complete syntax:AddUser server,user,pwd,expires,homedir,comment,scriptdir672Part V:Accessing Hidden ComponentsII4684-8 ch24.f.qc 3/3/00 9:45 AM Page 672Table 24-3AddUser Paramet
21、ersArgumentDescriptionserverName of server or“”for local serveruserName of user accountpwdPasswordexpiresDays the password is validhomedirHome directorycommentComment describing the account purposescriptdirScript directoryDeleting user accountsDelUserdeletes a user account.Note that deleting a user
22、account cant beundone.Even if you re-create the account later,the new account gets newSecurity IDs and acts as a different account.24-5.VBSset tool=CreateObject(“nt.user”)if tool.DelUser(“”,“testaccount”)thenMsgBox“User account deleted!”elseMsgBox“Couldnt delete user account:“&tool.GetLastErrorend i
23、fChanging passwordsYour script can change user passwords,too.You have two options,one beingto provide the old password as authentication.Or,if you are an administra-tor,you can use administrative override and skip the old password.This isespecially useful if someone has forgotten his or her password
24、 or has left thecompany.Execute script 24-4.VBSfirst to add a test user account,and then change thepassword:24-6.VBSset tool=CreateObject(“nt.user”)if tool.ChangePassword(“”,“testaccount”,_“newpassword”,“secret”)thenMsgBox“Password has changed!”elseChapter 24:Managing Windows NT/2000 Server673II4684
25、-8 ch24.f.qc 3/3/00 9:45 AM Page 673MsgBox“Couldnt change password:“&tool.GetLastErrorend ifTo use administrative override,you need administrator privilege.Skip the oldpassword:24-7.VBSset tool=CreateObject(“nt.user”)if tool.ChangePassword(“”,“testaccount”,_“newpassword”)thenMsgBox“Password has chan
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Windows 脚本 编程 核心技术 Chapter24
限制150内