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

时间:2014-01-17 17:39:42

标签: excel vba excel-vba vbscript

我创建了一个脚本,该脚本将从Excel工作表中导出数据并创建AD用户。如果我在脚本中设置密码,一切都很完美;但我不想将密码放在脚本中,我需要它从工作表中提取密码。每当我尝试引用包含密码的单元格时,我都会收到错误消息:对象不支持此属性或方法:'objUser.SetPassword'就像我说的那样,我可以毫无问题地拉出其他所有内容并创建整个帐户但无法通过设置密码错误。第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

1 个答案:

答案 0 :(得分:2)

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

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

密码设置:

objUser.SetPassword("Excel Cell Value here")
相关问题