I cant get the user in my active directory with this code

  adsi, ldap, vbscript, windows, windows-server-2008

I’ve been trying to make a small code for a project I have on windows server 2008. The person who uses this script has 3 options:

1 – Search for a user (Working)

2 – Activate ‘Password never expires’

3 – Return basic user info.

Number 1 is completely working, but I ran into a problem in 2 and 3 where for some reason I can’t find the users. I’ve tried printing out the variables that should have the user’s names, but it came out blank. Im really new to this so it might be something pretty stupid, any help is appreciated, thanks!

Code:

On Error Resume Next
Dim sInput
Dim user
existe = False
sInput = InputBox("Selecione uma opcao:" + vbNewLine + "1 - Pesquisar user" + vbNewLine + "2 - Ativar 'Password never expires'" + vbNewLine + "3 - Retornar informacao basica do user")

Select Case sInput
    Case 1
        Dim existe
        Set FSO = CreateObject("Scripting.FileSystemObject")
        user = InputBox("Insira o nome do user:")
        user = "C:Users" + user
        For Each objFolder In FSO.GetFolder("C:Users").SubFolders
    
            If user = objFolder Then
                WScript.Echo "O user existe"
                existe = True
            End If
        Next

        If existe = False Then
            WScript.Echo "O user não existe"
        End If
    
    Case 2
    user = InputBox("Insira o nome do user:")
    Const ADS_UF_DONT_EXPIRE_PASSWD = &h10000
     
    Set objUser = GetObject _
        ("LDAP://cn=" + user + ",ou=Users,dc=fabrikam,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
    
    Case 3
    user = InputBox("Insira o nome do user:")
    On Error Resume Next

    Set objUser = GetObject _
        ("LDAP://cn=" + user + ",ou=Users,dc=NA,dc=fabrikam,dc=com")
     
    WScript.Echo "User Principal Name: " & objUser.userPrincipalName
    WScript.Echo "SAM Account Name: " & objUser.sAMAccountName
    WScript.Echo "User Workstations: " & objUser.userWorkstations

    Set objDomain = GetObject("LDAP://dc=NA,dc=fabrikam,dc=com")
    WScript.Echo "Domain controller: " & objDomain.dc

    Case Else
        WScript.Echo sInput + " não é uma opção válida!"
End Select

Source: Windows Questions

LEAVE A COMMENT