Question : i want to automate the process of cleaning up my active directory

i want to automate the process of cleaning up my active directory. i have some 1500 computer accounts that are stale in my active directory. i want to clean them up. so firstly i want to run a script to get a list of computers that are stale. secondly i want to take only 50 computers from the list and test if they are still on the network, like for example ping the computer names. then i want to disable those 50 computers and move it to an ou. i will keep them there for another 30 days before deleting. maybe just an idea, create script 1 to get a list of computers. then cut and paste 50 computers from the list to another text file and use the text file to check if these computers are stlll online by anychance. if no then i want to use the text file containing the 50 computesr to disable the computers and move them to a specific OU. the every month i will delete the obselete accounts manually. need help in creating those scripts and also how to run the scripts

Answer : i want to automate the process of cleaning up my active directory

I see the mistake - this should disable the accounts that cannot be pinged.
Edit;
strTargetOU = "ou=Inactive,dc=MyDomain,dc=com"
so that it is a match to the location of the OU from step 2.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
Option Explicit
 
Const ForReading = 1
 
Dim strFilePath, objFSO, objLogFSO, objFile, objLogFile, adoConnection, adoCommand
Dim objRootDSE, strDNSDomain, strFilter, strQuery, adoRecordset
Dim strComputerDN, objShell, lngBiasKey, lngBias
Dim objDate, dtmPwdLastSet, k
Dim intDays, strTargetOU, objTargetOU, objComputer
Dim intTotal, intInactive, intNotMoved, intNotDisabled
Dim InitFSO
Dim strNextLine
Dim arrComputerList
Dim dtDate, strDate
Dim strLogFilePath
Dim strImportFilePath
Dim i
Dim strComputerStatus
Dim strPingResult
Dim oPingResult
Dim cPingResults
Dim sHost
 
sHost = "."
 
dtDate = now()
strDate = Trim(mid(dtDate,1,InStr(dtDate," ")))
strDate = replace(strDate,"/","-")
 
' Specify the log file. This file will be created if it does not
' exist. Otherwise, the program will append to the file.
strLogFilePath = "Step-3-Ping-Old-Computers-" & strDate & ".st3"
 
' Specify the Distinguished Name of the Organizational Unit into
' which inactive computer objects will be moved.
strTargetOU = "ou=Inactive,dc=MyDomain,dc=com"
 
Set ObjFSO = CreateObject("UserAccounts.CommonDialog")
ObjFSO.Filter = "Step 2|*.st2|Text Documents|*.txt|All Files|*.*"
ObjFSO.FilterIndex = 1
InitFSO = ObjFSO.ShowOpen
 
If InitFSO = False Then
    Wscript.Echo "Script Error: Please select a file!"
    Wscript.Quit
Else
    strImportFilePath = ObjFSO.FileName
End If
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set objFile = objFSO.OpenTextFile(strImportFilePath, 8, True, 0)
If (Err.Number <> 0) Then
    On Error GoTo 0
    Wscript.Echo "File " & strImportFilePath & " cannot be opened"
    Set objFSO = Nothing
    Wscript.Quit
End If
On Error GoTo 0
 
Set objLogFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set objLogFile = objLogFSO.OpenTextFile(strLogFilePath, 8, True, 0)
If (Err.Number <> 0) Then
    On Error GoTo 0
    Wscript.Echo "File " & strLogFilePath & " cannot be opened"
    Set objFSO = Nothing
    Wscript.Quit
End If
On Error GoTo 0
 
Set objFile = objFSO.OpenTextFile (strImportFilePath, ForReading)
 
Do Until objFile.AtEndOfStream
    strNextLine = objFile.Readline
    arrComputerList = Split(strNextLine, vbTab)
    For i = 1 to Ubound(arrComputerList)
        strComputerStatus = arrComputerList(0)
        strComputerDN = arrComputerList(1)
        strComputerDN = mid(strComputerDN,4,InStr(strComputerDN,",")-4)
        ' Ping computer if moved
        On Error Resume Next
        if trim(strComputerStatus) = "Moved:" then
            'Run PING command
            Set cPingResults = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
                   sHost & "/root/cimv2"). ExecQuery("SELECT * FROM Win32_PingStatus " & _
                   "WHERE Address = '" + strComputerDN + "'")
            'Take ping reults and put into variable strPingResult 
            strPingResult = 0
            For Each oPingResult In cPingResults
                strPingResult = oPingResult.ResponseTime
            Next
    
            'Catch PINGS that do not have a result - typically this is for unreachable devices
            if IsEmpty(strPingResult) then
                strPingResult = 9999
            end if
            if IsNULL(strPingResult) then
                strPingResult = 9999
            end if
        
            ' Run ping again if first attempt fails
            if strPingResult = 9999 then
                'Run PING command
                Set cPingResults = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
                       sHost & "/root/cimv2"). ExecQuery("SELECT * FROM Win32_PingStatus " & _
                       "WHERE Address = '" + strComputerDN + "'")
                'Take ping reults and put into variable strPingResult 
                strPingResult = 0
                For Each oPingResult In cPingResults
                    strPingResult = oPingResult.ResponseTime
                Next
                
                'Catch PINGS that do not have a result - typically this is for unreachable devices
                if IsEmpty(strPingResult) then
                    strPingResult = 9999
                end if
                if IsNULL(strPingResult) then
                    strPingResult = 9999
                end if
            end if
            if strPingResult = 9999 then
                'Cannot reach client machine
                objLogFile.WriteLine strComputerDN & vbTab & "Offline"
                'Disable computer
                Set objComputer = GetObject("LDAP://cn=" & strComputerDN & "," & strTargetOU, vbNullString)
                objComputer.AccountDisabled = True
                objComputer.SetInfo
            else
                'Client machine online
                objLogFile.WriteLine strComputerDN & vbTab & "Online"
            end if
        end if
    Next
Loop
 
' Clean up.
objFile.Close
objLogFile.Close
adoConnection.Close
Set objFile = Nothing
Set objFSO = Nothing
Set objLogFile = Nothing
Set objLogFSO = Nothing
Set objShell = Nothing
Set objComputer = Nothing
 
Wscript.Echo "Done"
Open in New Window Select All
Random Solutions  
 
programming4us programming4us