none
Help Vbs script script to delete AD users, profils and home drives from a file

    Question

  • Hello everyone,
    sorry if my English is not perfect.

    I seek the help of a vbs script.

     I would like to delete multiple users in Active Directory from a TXT file to which I have added the account  login.

    Set objFSO = CreateObject("Scripting.FileSystemObject")
     
    set objListe = objFSO.OpenTextFile("utilisateurs.txt")
     
    Do Until objListe.AtEndOfStream
     
    NomUtilisateur = objListe.Readline
     
    Set MonOU = GetObject("LDAP://ou=test,dc=xxx,dc=lan")
     
    MonOU.Delete "user", "cn=" & NomUtilisateur
     
    Loop
    


    I have tried to make a script here based on several script via "ADSI Scriptomatic" and changing some variable it works but it is not perfect
    Below is the script

     

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.DeleteFolder("chemin vers le profil\" & samaccountname)
    objFSO.DeleteFolder("chemin vers le profil TS\" & samaccountname)
    objFSO.DeleteFolder("chemin vers le dossier data\" & samaccountname)
    loop
    that it automatically removes the sharing (profils and home drives)
    I did not need to add the script.

     

    thank you


    bonjour a tous,

    je cherche de l aide sur un script vbs.

    j aimerai pouvoir faire une suppression en masse d'utilisateurs dans l active directory a partir d un fichier TXT dans lequel j ai rajouté le login d'ouverture de session.

    j ai essayé de faire un script en me basant sur plusieurs script via "adsi scriptomatic" et en modifiant quelque variable cela marche mais j aimerai pouvoir l ameliorer.

    ci dessous le script

     

     

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    set objListe = objFSO.OpenTextFile("utilisateurs.txt")

    Do Until objListe.AtEndOfStream

    NomUtilisateur = objListe.Readline

    Set MonOU = GetObject("LDAP://ou=test,dc=xxx,dc=lan")

    MonOU.Delete "user", "cn=" & NomUtilisateur

    Loop
    j aimerai en faite si cela est possible que le script va lire les information sur le compte de l user dans AD et en regardant les champs ci dessous

     

     Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.DeleteFolder("chemin vers le profil\" & samaccountname)
    objFSO.DeleteFolder("chemin vers le profil TS\" & samaccountname)
    objFSO.DeleteFolder("chemin vers le dossier data\" & samaccountname)
    loop
    qu il supprime automatiquement les donnés sur le partage sans que j ai besoin de les rajouter sur le script.


    Merci d'avance pour votre aide
    • Edited by Djthoz91 Friday, November 04, 2011 9:48 AM
    Thursday, November 03, 2011 7:44 PM

Answers

  • I'm going to make some assumptions. I assume the text file has the "pre-Windows 2000 logon" names of users. You want to delete the corresponding user objects from AD, plus delete their profile path, their Terminal Services profile path, and their home directory. The following VBScript program (not tested) should be a good start:

     

    Option Explicit

    Dim strFile, objFSO, objFile, objRootDSE, strDNSDomain, objTrans
    Dim strNetBIOSDomain, strName, strUserDN, objUser

    Const ForReading = 1
    ' Constants for the NameTranslate object.
    Const ADS_NAME_INITTYPE_GC = 3
    Const ADS_NAME_TYPE_NT4 = 3
    Const ADS_NAME_TYPE_1779 = 1

    ' Specify the file of user account logins.
    strFile = "utilisateurs.txt"

    ' Open the file for reading.
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(strFile, ForReading)

    ' Determine DNS name of domain from RootDSE.
    Set objRootDSE = GetObject("LDAP://RootDSE")
    strDNSDomain = objRootDSE.Get("defaultNamingContext")

    ' Use the NameTranslate object to find the NetBIOS domain name from the
    ' DNS domain name.
    Set objTrans = CreateObject("NameTranslate")
    objTrans.Init ADS_NAME_INITTYPE_GC, ""
    objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain
    strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)
    ' Remove trailing backslash.
    strNetBIOSDomain = Left(strNetBIOSDomain, Len(strNetBIOSDomain) - 1)

    ' Read user "pre-Windows 2000 logon" names from file.
    Do Until objFile.AtEndOfStream
        strName = Trim(objFile.ReadLine)
        ' Skip blank lines.
        If (strName <> "") Then
            ' Use the Set method to specify the NT format of the user name.
            ' Trap error if user does not exist.
            On Error Resume Next
            objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strName
            If (Err.Number <> 0) Then
                On Error GoTo 0
                Wscript.Echo "User " & strName & " does not exist"
            Else
                On Error GoTo 0
                ' Use the Get method to retrieve the RPC 1779 Distinguished Name.
                strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
                ' Bind to the user object.
                Set objUser = GetObject("LDAP://" & strUserDN)
                ' Delete user profile path.
                If (objUser.profilePath <> "") Then
                    objFSO.DeleteFolder(objUser.profilePath)
                End If
                ' Delete user TS profile path.
                If (objUser.msTSProfilePath <> "") Then
                    objFSO.DeleteFolder(objUser.msTSProfilePath)
                End If
                ' Delete user home directory.
                If (objUser.homeDirectory <> "") Then
                    objFSO.DeleteFolder(objUser.homeDirectory)
                End If
                ' Delete the user object from AD.
                objUser.DeleteObject (0)
            End If
        End If
    Loop

    ' Clean up.
    objFile.Close

    -----

     


    Richard Mueller - MVP Directory Services
    Monday, November 21, 2011 12:01 AM
    Moderator

All replies

  • Since no one has answered yet, I'll take a stab at it (my VBScript is somewhat lacking):

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    set objListe = objFSO.OpenTextFile("utilisateurs.txt")
    Do Until objListe.AtEndOfStream
    NomUtilisateur = objListe.Readline
    Set MonOU = GetObject("LDAP://ou=test,dc=xxx,dc=lan")
    MonOU.Delete "user", "cn=" & NomUtilisateur
    objFSO.DeleteFolder("chemin vers le profil\" & NomUtilisateur)
    objFSO.DeleteFolder("chemin vers le profil TS\" & NomUtilisateur)
    objFSO.DeleteFolder("chemin vers le dossier data\" & NomUtilisateur)
    Loop
    



    Rich Prescott | Infrastructure Architect, Windows Engineer and PowerShell blogger | MCITP, MCTS, MCP

    Engineering Efficiency
    @Rich_Prescott
    Client System Administration tool
    AD User Creation tool
    Tuesday, November 08, 2011 12:43 AM
    Moderator
  • even with an account technet THERE no answer ...
    Saturday, November 12, 2011 7:16 PM
  • I'm going to make some assumptions. I assume the text file has the "pre-Windows 2000 logon" names of users. You want to delete the corresponding user objects from AD, plus delete their profile path, their Terminal Services profile path, and their home directory. The following VBScript program (not tested) should be a good start:

     

    Option Explicit

    Dim strFile, objFSO, objFile, objRootDSE, strDNSDomain, objTrans
    Dim strNetBIOSDomain, strName, strUserDN, objUser

    Const ForReading = 1
    ' Constants for the NameTranslate object.
    Const ADS_NAME_INITTYPE_GC = 3
    Const ADS_NAME_TYPE_NT4 = 3
    Const ADS_NAME_TYPE_1779 = 1

    ' Specify the file of user account logins.
    strFile = "utilisateurs.txt"

    ' Open the file for reading.
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(strFile, ForReading)

    ' Determine DNS name of domain from RootDSE.
    Set objRootDSE = GetObject("LDAP://RootDSE")
    strDNSDomain = objRootDSE.Get("defaultNamingContext")

    ' Use the NameTranslate object to find the NetBIOS domain name from the
    ' DNS domain name.
    Set objTrans = CreateObject("NameTranslate")
    objTrans.Init ADS_NAME_INITTYPE_GC, ""
    objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain
    strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)
    ' Remove trailing backslash.
    strNetBIOSDomain = Left(strNetBIOSDomain, Len(strNetBIOSDomain) - 1)

    ' Read user "pre-Windows 2000 logon" names from file.
    Do Until objFile.AtEndOfStream
        strName = Trim(objFile.ReadLine)
        ' Skip blank lines.
        If (strName <> "") Then
            ' Use the Set method to specify the NT format of the user name.
            ' Trap error if user does not exist.
            On Error Resume Next
            objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strName
            If (Err.Number <> 0) Then
                On Error GoTo 0
                Wscript.Echo "User " & strName & " does not exist"
            Else
                On Error GoTo 0
                ' Use the Get method to retrieve the RPC 1779 Distinguished Name.
                strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
                ' Bind to the user object.
                Set objUser = GetObject("LDAP://" & strUserDN)
                ' Delete user profile path.
                If (objUser.profilePath <> "") Then
                    objFSO.DeleteFolder(objUser.profilePath)
                End If
                ' Delete user TS profile path.
                If (objUser.msTSProfilePath <> "") Then
                    objFSO.DeleteFolder(objUser.msTSProfilePath)
                End If
                ' Delete user home directory.
                If (objUser.homeDirectory <> "") Then
                    objFSO.DeleteFolder(objUser.homeDirectory)
                End If
                ' Delete the user object from AD.
                objUser.DeleteObject (0)
            End If
        End If
    Loop

    ' Clean up.
    objFile.Close

    -----

     


    Richard Mueller - MVP Directory Services
    Monday, November 21, 2011 12:01 AM
    Moderator
  • This is exactly what I was looking for, In particular, the RPC 1779) Distinguished name call.  My script just wasn't working without it.

    Thanks also for the explanations.  I'm fairly new to scripting and I like to actually understand what the code does.

    Tuesday, August 20, 2013 10:02 PM