Dim arrFileLines() ,n
i = 0
Const ForReading = 1
strInputFile="C:\BAU_Users.txt" '需要建立用户账户所在的文件
Set FSO=CreateObject("Scripting.FileSystemObject")
Set objFile=FSO.OpenTextFile(strInputFile,ForReading)
Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName '得到本地计算机名
Set colAccounts = GetObject("WinNT://" & strComputer & "")
colAccounts.Filter = Array("group") '提取本地组信息
For Each objGroup In colAccounts '检查本地组中是否存在pcAnywhere_users组
If objGroup.Name = pcAnywhere_users Then
intExists = 2
End If
Next
Set objGroup = colAccounts.Create("group", "pcAnywhere_users") '建立pcAnywhere_users组
objGroup.SetInfo
Do Until objFile.AtEndOfStream '读取文件每一行内容到arrFileLine()数组中
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
loop
objFile.Close '关闭打开的文件
For n = Ubound(arrFileLines) to LBound(arrFileLines) Step -1 '按照文件中的用户账户名创建账户
strAccount =arrFileLines(n)
strPswd = "viso1234" '并为每个新建用户创建初始密码"viso1234"
' check if local account already exists
intExists = 0
colAccounts.Filter = Array("user") '检查本地是否存在即将建立的用户账户
For Each objUser In colAccounts
If objUser.Name = strAccount Then
intExists = 1
End If
Next
If intExists = 0 and n <9 Then '如果账户不存在,并且是文件的前8行的用户名,就创建用户并设置初始密码
Set objUser = colAccounts.Create("user", strAccount)
objUser.SetPassword strPswd
objUser.SetInfo
' Must change pswd next login
objUser.PasswordExpired = 1 '新建的账户必须在首次登录时需重设密码
objUser.SetInfo
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group")
Set objUser = GetObject("WinNT://" & strComputer & "/" & strAccount & ",user")
objGroup.Add(objUser.ADsPath) '将新建账户加入本地管理员组中
Set objGroup = GetObject("WinNT://" & strComputer & "/pcAnywhere_users,group")
Set objUser = GetObject("WinNT://" & strComputer & "/" & strAccount & ",user")
objGroup.Add(objUser.ADsPath) '将新建账户加入pcAnywhere_users组中
else
Set objUser = colAccounts.Create("user", strAccount)
' set pswd
objUser.SetPassword strPswd
objUser.SetInfo
' Must change pswd next login
objUser.PasswordExpired = 1
objUser.SetInfo '从第9行开始,只要本地不存在的账户都一一建立
' Add to pcAnywhere_users group
Set objGroup = GetObject("WinNT://" & strComputer & "/pcAnywhere_users,group")
Set objUser = GetObject("WinNT://" & strComputer & "/" & strAccount & ",user")
objGroup.Add(objUser.ADsPath)
End If
Next