如何使用vbscript和excel工作表将密码设置为Active Directory帐户

我已经创build了一个脚本,将从Excel工作表中导出数据并创build一个AD用户。 如果我在脚本中设置密码,一切正常; 但我不想在脚本中放置密码,我需要它从工作表中拉密码。 每当我尝试引用包含密码的单元格时,我都会收到错误消息:对象不支持此属性或方法:'objUser.SetPassword'正如我所说,我可以将其他所有内容都解决,并创build整个帐户但不能通过设定的密码错误。 35行是错误的地方。

' Create User Accounts Based on Information in a Spreadsheet Const ADS_ACETYPE_ACCESS_DENIED_OBJECT = &H6 Const ADS_ACEFLAG_OBJECT_TYPE_PRESENT = &H1 Const CHANGE_PASSWORD_GUID = "{ab721a53-1e2f-11d0-9819-00aa0040529b}" Const ADS_RIGHT_DS_CONTROL_ACCESS = &H100 Const ADS_UF_DONT_EXPIRE_PASSWD = &h10000 Const ADS_PROPERTY_APPEND = 3 Const ADS_PROPERTY_DELETE = 4 Set objExcel = CreateObject("Excel.Application") Set objWorkbook = objExcel.Workbooks.Open _ ("C:\New_users.xlsx") intRow = 2 Do Until objExcel.Cells(intRow,1).Value = "" Set objOU = GetObject("LDAP://ou=Share Point Users,dc=com") Set objUser = objOU.Create _ ("User", "cn=" & objExcel.Cells(intRow, 1).Value) objUser.GivenName = objExcel.Cells(intRow, 2).Value objUser.SN = objExcel.Cells(intRow, 3).Value objuser.displayName = objExcel.Cells(intRow, 4).Value objuser.description = objExcel.Cells(intRow, 5).Value objuser.mail = objExcel.Cells(intRow, 6).Value objuser.userprincipalName = objExcel.Cells(intRow, 7).Value objuser.samAccountName = objExcel.Cells(intRow, 8).Value objUser.SetInfo 'Set Users password and enable account Set objUser = GetObject _ ("LDAP://cn=" & objExcel.Cells(intRow, 1).Value & ",ou=Share Point Users,dc=com") objuser.SetPassword = objExcel.Cells(intRow, 9).Value 'this where the error occurs objUser.AccountDisabled = False objuser.SetInfo ' Prevent Users From Changing Their Passwords Set objUser = GetObject _ ("LDAP://cn=" & objExcel.Cells(intRow, 1).Value & ",ou=Share Point Users,dc=com") Set objSD = objUser.Get("ntSecurityDescriptor") Set objDACL = objSD.DiscretionaryAcl arrTrustees = array("nt authority\self", "EVERYONE") For Each strTrustee in arrTrustees Set objACE = CreateObject("AccessControlEntry") objACE.Trustee = strTrustee objACE.AceFlags = 0 objACE.AceType = ADS_ACETYPE_ACCESS_DENIED_OBJECT objACE.Flags = ADS_ACEFLAG_OBJECT_TYPE_PRESENT objACE.ObjectType = CHANGE_PASSWORD_GUID objACE.AccessMask = ADS_RIGHT_DS_CONTROL_ACCESS objDACL.AddAce objACE Next objSD.DiscretionaryAcl = objDACL objUser.Put "nTSecurityDescriptor", objSD objUser. SetInfo 'Set password to never expire Set objUser = GetObject _ ("LDAP://cn=" & objExcel.Cells(intRow, 1).Value & ",ou=Share Point Users,dc=com") intUAC = objUser.Get("userAccountControl") If ADS_UF_DONT_EXPIRE_PASSWD AND intUAC Then Wscript.Echo "Already enabled" Else objUser.Put "userAccountControl", intUAC XOR _ ADS_UF_DONT_EXPIRE_PASSWD objUser.SetInfo WScript.Echo "Password never expires is now enabled" End If ' Add a User to Domain Guest and SharepointUserOnlyGroup 'Also sets Domain Guest as the primary group Set objGroup = GetObject _ ("LDAP://cn=Domain Guests,cn=Users,dc=com") objGroup.GetInfoEx Array("primaryGroupToken"), 0 intPrimaryGroupToken = objGroup.Get("primaryGroupToken") objGroup.PutEx ADS_PROPERTY_APPEND, _ "member", Array("cn=" & objExcel.Cells(intRow, 1).Value & ",ou=Share Point Users,dc=com") objGroup.SetInfo objUser.Put "primaryGroupID", intPrimaryGroupToken objUser.SetInfo Set objGroup = GetObject _ ("LDAP://cn=SharePointOnlyGroup,ou=Groups-Security,dc=com") objGroup.PutEx ADS_PROPERTY_APPEND, _ "member", Array("cn=" & objExcel.Cells(intRow, 1).Value & ",ou=Share Point Users,ou=St. Louis,ou=Sites-US,dc=stl2,dc=mddcpa,dc=com") objGroup.SetInfo ' Remove a User from Domain Users group Set objGroup = GetObject _ ("LDAP://cn=Domain Users,cn=Users,dc=stl2,dc=mddcpa,dc=com") objGroup.PutEx ADS_PROPERTY_DELETE, _ "member",Array("cn=" & objExcel.Cells(intRow, 1).Value & ",ou=Share Point Users,dc=com") objGroup.SetInfo intRow = intRow + 1 Loop objExcel.Quit 

您需要一个Worksheet对象来获取单元格值。

 Set oWS = objWorkbook.Worksheets("Sheet1") ' Replace all occurances of "objExcel.Cells(" to "oWS.Cells(" 

对于密码设置:

 objUser.SetPassword("Excel Cell Value here")