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"
|