我使用下面的VBS脚本创建了一个本地用户帐户并将其添加到管理员组,但是当我运行它时,它会创建帐户但是当我尝试将其添加到帐户时会出现异常本地管理组。
Const ADS_UF_DONT_EXPIRE_PASSWD = &h10000
' specify account to create
strAccount = "user"
strPswd = "password"
' get local computer name
Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName
' check if local account already exists
intExists = 0
Set colAccounts = GetObject("WinNT://" & strComputer & "")
colAccounts.Filter = Array("user")
For Each objUser In colAccounts
If objUser.Name = strAccount Then
intExists = 1
End If
Next
If intExists = 0 Then
' create local user
Set colAccounts = GetObject("WinNT://" & strComputer & "")
Set objUser = colAccounts.Create("user", strAccount)
' set pswd
objUser.SetPassword strPswd
objUser.SetInfo
' add to local admins group
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group")
Set objUser = GetObject("WinNT://" & strComputer & "/" & strAccount & ",user")
objGroup.Add(objUser.ADsPath)
' set password to not expire
intUserFlags = objUser.Get("UserFlags")
objUser.put "Userflags", intUserFlags Or ADS_UF_DONT_EXPIRE_PASSWD
objUser.SetInfo
End If
这是我得到的错误消息:
答案 0 :(得分:1)
GetObject("WinNT://" & strComputer & "")
中缺少某些内容。使用
' create local user
Set colAccounts = GetObject("WinNT://" & strComputer & ",computer")
Set objUser = colAccounts.Create("user", strAccount)
没有精力和毅力去分析它更深或更深:这是我的(cca 10岁)剧本:
' VB Script Document - create & manage a local user account
option explicit
On Error Goto 0
Dim WshShell, WshNetwork, objGroup, objComputer, objUser _
, strUserObjectName, strUserObjectPass , strUserObjectDesc, strUserObjectFull _
, strResult, strCOMPUTERNAME, strCurrentUser, _
, lngUserProperties, lngUF
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objGroup = Nothing
strResult = ""
strCOMPUTERNAME = WshNetwork.ComputerName
strCurrentUser = WshNetwork.UserName
If AmIAnAdmin() Then
Else
'''
Wscript.Echo strResult, "I am not an Admin"
Wscript.Quit
End If
'******************************************************'
'************ CHANGE NEXT FOUR LINES *****************'
strUserObjectName = "Student"
strUserObjectPass = "P@ssw0rd"
strUserObjectDesc = "a student of our school"
strUserObjectFull = "new student"
'************ CHANGE ABOVE FOUR LINES *****************'
'******************************************************'
Set objComputer = GetObject("WinNT://" & strCOMPUTERNAME & ",computer")
' Turns error processing on, disables error prompts in the interface
' and allows the script to continue
On Error Resume Next
' try to connect to user object to see if account is a local user
Set objUser = objComputer.GetObject( "user", strUserObjectName)
If Err.Number = 0 Then
' local user exists
On Error Goto 0
strResult = "Updated "
' Do not change Password of existing local account
'''objUser.SetPassword strUserObjectPass
' Set account so its not disabled
objuser.accountdisabled = False
' Set account so its not locked (unlock account)
objUser.IsAccountLocked = False
lngUserProperties = setUserProperties( False)
'Activate the above settings
objUser.SetInfo
Else
' local user does not exist -> create it
On Error Goto 0
strResult = "Created "
'Create account and populate account info
Set objUser = objComputer.Create( "user", strUserObjectName)
objUser.SetPassword strUserObjectPass
objUser.SetInfo
objUser.FullName = strUserObjectFull
objUser.Description = strUserObjectDesc
lngUserProperties = setUserProperties( True)
'Activate the above settings
objUser.SetInfo
End If
Err.Clear 'Clears any error numbers returned from above lines
'Add account to desired groups
addAccoutToALocalGroup "Users", Not booRemoveMembership
addAccoutToALocalGroup "Power Users", Not booRemoveMembership
addAccoutToALocalGroup "Debugger Users", Not booRemoveMembership
addAccoutToALocalGroup "Administrators", Not booRemoveMembership
lngUF = objUser.Get( "userFlags")
'''
Wscript.echo strResult, strCOMPUTERNAME, strUserObjectName _
, "0x" & Hex( lngUserProperties)
'''
Function setUserProperties( ByVal booNewUser)
lngUF = objUser.Get( "userFlags")
If booNewUser Then
''' force user to change password at next logon
lngUF = lngUF Or ADS_UF_PASSWORD_EXPIRED
Else
End If
' Lock out Account? Or need to be used objUser.IsAccountLocked = True?
'lngUF = lngUF Or ADS_UF_LOCKOUT
' Disable Account? Or need to be used objuser.accountdisabled = True?
'lngUF = lngUF Or ADS_UF_ACCOUNTDISABLE
' Set User so he cannot change password
'lngUF = lngUF Or ADS_UF_PASSWD_CANT_CHANGE
' Set Password so it doesn't expire
'lngUF = lngUF Or ADS_UF_DONT_EXPIRE_PASSWD
' Set Account Password is not required (allow empty password)
'lngUF = lngUF Or ADS_UF_PASSWD_NOTREQD
''' Or ADS_UF_Unknown ''' not valid trying {objUser.SetInfo}
objUser.Put "userFlags", lngUF
setUserProperties = lngUF
End Function ''' setUserProperties
' Adds account to a local group;
' returns zero if successfull; otherwise, returns error number
Function addAccoutToALocalGroup( strLocalGroup, booRemove)
On Error Resume Next
' try to connect to group object
Set objGroup = GetObject("WinNT://" & strComputerName _
& "/" & strLocalGroup & ",group")
addAccoutToALocalGroup = Err.Number
Select Case addAccoutToALocalGroup
Case 0
' local group exists
On Error Goto 0
If booUserIsInGroupAlready() Then
If booRemove Then
objGroup.Remove( objUser.ADsPath)
Else
End If
Else
If booRemove Then
Else
objGroup.Add( objUser.ADsPath)
End If
End If
Case -2147022676
' local group does not exist
''Wscript.Echo "local group does not exist:" & strLocalGroup
On Error Goto 0
Case Else
' another error occured
''Wscript.Echo "Error # " & Hex(Err.Number) & " " & _
'' CStr(Err.Number) & " " & Err.Description & vbNewLine & _
'' "on user " & strUserObjectName & " " & sUserDesc & vbNewLine & _
'' "computer " & strComputerName & vbNewLine & _
'' "on group " & strLocalGroup
On Error Goto 0
End Select
End Function ''' addAccoutToALocalGroup
Function booUserIsInGroupAlready
booUserIsInGroupAlready = False
Dim objUserLoc
For Each objUserLoc In objGroup.Members
If StrComp( objUserLoc.Name, strUserObjectName, vbTextCompare) = 0 Then
' strUserObjectName & " is already a member of this group: " & objGroup.Name
booUserIsInGroupAlready = True
Exit For
Else
End If
Next 'objUserLoc
End Function ''' booUserIsInGroupAlready
Function AmIAnAdmin
Set objGroup = GetObject("WinNT://" & strComputerName & "/Administrators,group")
AmIAnAdmin = False
Dim objUserLoc
For Each objUserLoc In objGroup.Members
If StrComp( objUserLoc.Name, strCurrentUser, vbTextCompare) = 0 Then
AmIAnAdmin = True
Exit For
Else
End If
Next 'objUserLoc
Set objGroup = Nothing
End Function ''' AmIAnAdmin
CONST booRemoveMembership = True
'
'BEGININING OF FILE http://www.novell.com/coolsolutions/trench/659.html#tonyp
'
Const ADS_UF_SCRIPT = 1 ' 0x1
Const ADS_UF_ACCOUNTDISABLE = 2 ' 0x2
Const ADS_UF_unknown = 4 ' 0x4
Const ADS_UF_HOMEDIR_REQUIRED = 8 ' 0x8
Const ADS_UF_LOCKOUT = 16 ' 0x10
Const ADS_UF_PASSWD_NOTREQD = 32 ' 0x20
Const ADS_UF_PASSWD_CANT_CHANGE = 64 ' 0x40
Const ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = 128 ' 0x80
Const ADS_UF_TEMP_DUPLICATE_ACCOUNT = 256 ' 0x100
Const ADS_UF_NORMAL_ACCOUNT = 512 ' 0x200
Const ADS_UF_INTERDOMAIN_TRUST_ACCOUNT = 2048 ' 0x800
Const ADS_UF_WORKSTATION_TRUST_ACCOUNT = 4096 ' 0x1000
Const ADS_UF_SERVER_TRUST_ACCOUNT = 8192 ' 0x2000
Const ADS_UF_DONT_EXPIRE_PASSWD = 65536 ' 0x10000
Const ADS_UF_MNS_LOGON_ACCOUNT = 131072 ' 0x20000
Const ADS_UF_SMARTCARD_REQUIRED = 262144 ' 0x40000
Const ADS_UF_TRUSTED_FOR_DELEGATION = 524288 ' 0x80000
Const ADS_UF_NOT_DELEGATED = 1048576 ' 0x100000
Const ADS_UF_USE_DES_KEY_ONLY = 2097152 ' 0x200000
Const ADS_UF_DONT_REQUIRE_PREAUTH = 4194304 ' 0x400000
Const ADS_UF_PASSWORD_EXPIRED = 8388608 ' 0x800000
Const ADS_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION = 16777216 ' 0x1000000
'
' default userFlags value = 0x201
'
'END OF FILE http://www.novell.com/coolsolutions/trench/659.html#tonyp
'END OF FILE