Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for Natalie Green's createlistenabledhosts.vbs

(view source code of ng_createlistenabledhosts.vbs as plain text)

  1. ' http://morgansimonsen.wordpress.com/2012/02/28/script-to-find-outdated-computer-objects-in-active-directory/
  2. ' FindOutdatedComputers.vbs
  3. ' by Morgan Simonsen
  4. ' http://morgansimonsen.wordpress.com
  5. '
  6. ' This script will search an Active Directory domain for computer accounts that have
  7. ' not logged on the domain in the specified time limit (default 60 days).
  8. '
  9. ' 600 000 000 100-nanosecond intervals in 1 minute
  10. ' 1440 minutes in 24-hours
  11. ' 30 * 1440 * 600 000 000 = time in 100-nanosecond intervals since 1.1.1601
  12. '
  13. ' For Windows 2000, Windows XP and Windows Server 2003, the default computer account password change is 30 days,
  14. ' on Windows NT-based computers, the machine account password automatically changes every seven days
  15. '
  16. ' http://support.microsoft.com/kb/q154501/
  17. ' http://support.microsoft.com/default.aspx?scid=kb;en-us;q175468
  18. '
  19. ' While lastLogon is not replicated between domain controllers, attribute 'lastLogonTimestamp' IS replicated
  20. ' between DC's, so we do NOT have to check multiple DC's any more!! However, it is ONLY replicated every 14
  21. ' days, so it's used to find stale accounts.
  22. ' http://www.microsoft.com/technet/scriptcenter/topics/win2003/lastlogon.mspx
  23. '
  24. ' USAGE:
  25. ' cscript.exe CreateListEnabledHosts.vbs |1 only list or list and move objects, default is list only>
  26. '
  27. '===========================
  28. ' User changeable variables
  29. '===========================
  30. 'Time limit in number of days
  31. intTimeLimit = 45
  32.  
  33. Const adVarChar = 200
  34. Const MaxCharacters = 255
  35. Const ForReading = 1
  36. Const ForWriting = 2
  37. Const ADS_SCOPE_SUBTREE = 2
  38. Const FOR_WRITING = 2
  39.  
  40. Dim count, strOld, strDomain
  41. Set objRootDSE = GetObject("LDAP://RootDSE")
  42.  
  43. Set objArgs = WScript.arguments
  44. If objArgs.Count = 0 Then
  45. WScript.Echo "No arguments submitted, using default values"
  46. 	strDomain = objRootDSE.Get("defaultNamingContext")
  47. Else
  48. 	strDomain = objArgs.item(0)
  49. End If
  50.  
  51. WScript.Echo "strDomain=" & strDomain
  52. strDC = objRootDSE.Get("dnsHostName")
  53. WScript.Echo "strDC=" & strDC
  54.  
  55. strSearchFilter = "(&(objectCategory=computer)" _
  56.                 & "(operatingSystem=*)" _
  57.                 & "(!userAccountControl:1.2.840.113556.1.4.803:=2))"
  58. strAttributes = "name,distinguishedName,operatingSystemVersion,dNSHostName,operatingSystem" 'Comma separated
  59. strLevel = "subtree"
  60.  
  61. 'Set objWSHShell = WScript.CreateObject("WScript.Shell")
  62. set objFSO = CreateObject("Scripting.FileSystemObject")
  63. If objFSO.FileExists("Serverlist.txt") Then
  64. 	objFSO.DeleteFile("Serverlist.txt")
  65. End IF
  66. Set objFile = objFSO.CreateTextFile("Serverlist.txt", FOR_WRITING)
  67.  
  68. Dim strFSMOSchemaMaster
  69. Dim strFSMOInfrastructureMaster
  70. Dim strFSMOPDCEmulator
  71. Dim strFSMORIDMaster
  72. Dim strFSMODomainNamingMaster
  73.  
  74. 'WScript.Echo "time is:" & Now - #1/1/1601#
  75.  
  76. Set objADODBConnection = CreateObject("ADODB.Connection")
  77. objADODBConnection.Provider = "ADsDSOObject"
  78. objADODBConnection.Open
  79.  
  80. Set objADODBCommand = CreateObject("ADODB.Command")
  81. Set objADODBCommand.ActiveConnection = objADODBConnection
  82. objADODBCommand.Properties("Page Size") = 500
  83. 'objADODBCommand.CommandText = "<LDAP://" & strDC & "/" & strDomain & ">;" & strSearchFilter & ";" & strAttributes & ";" & strLevel
  84. objADODBCommand.CommandText = "<LDAP://" & strDomain & ">;" & strSearchFilter & ";" & strAttributes & ";" & strLevel
  85. Set objRecordSet = objADODBCommand.Execute
  86.  
  87. count = 0
  88.  
  89. While Not objRecordset.EOF
  90. 	'Wscript.Echo objRecordset.Fields("name")
  91. 	Call GetLastLogonTime(objRecordset.Fields("distinguishedName"))
  92. 	objRecordset.MoveNext
  93. Wend
  94.  
  95. WScript.Echo "Total number of computers in domain: " & objRecordset.RecordCount
  96. WScript.Echo "Number of computers that have not logged on in " & intTimeLimit & " days: " & count
  97.  
  98. objADODBConnection.Close
  99.  
  100. '***********************  Sort Server List  ****************************** 
  101. WScript.Echo "Sorting..."
  102.  
  103. Set DataList = CreateObject("ADOR.Recordset")
  104. DataList.Fields.Append "ComputerName", adVarChar, MaxCharacters
  105. DataList.Open
  106.  
  107. Set objFSO = CreateObject("Scripting.FileSystemObject")
  108. Set objFile = objFSO.OpenTextFile("serverlist.txt", ForReading)
  109.  
  110. Do Until objFile.AtEndOfStream
  111. 	strLine = objFile.ReadLine
  112. 	DataList.AddNew
  113. 	DataList("ComputerName") = strLine
  114. 	DataList.Update
  115. Loop
  116.  
  117. objFile.Close
  118.  
  119. DataList.Sort = "ComputerName"
  120.  
  121. DataList.MoveFirst
  122. Do Until DataList.EOF
  123. 	strText = strText & DataList.Fields.Item("ComputerName") & vbCrLf
  124. 	DataList.MoveNext
  125. Loop
  126.  
  127. Set objFile = objFSO.OpenTextFile("serverlist.txt", ForWriting)
  128.  
  129. objFile.WriteLine strText
  130. objFile.Close
  131.  
  132. WScript.Quit
  133.  
  134.  
  135. Function GetLastLogonTime(strComputerDN)
  136. 	On Error Resume Next
  137. 	Set objComputer = GetObject("LDAP://" & strDC & "/" & strComputerDN)
  138. 	'WScript.Echo "Computer: " & objComputer.cn
  139. 	Set objLogon = objComputer.Get("lastLogonTimestamp")
  140. 	intLogonTime = objLogon.HighPart * (2^32) + objLogon.LowPart
  141. 	intLogonTime = intLogonTime / (60 * 10000000)
  142. 	intLogonTime = intLogonTime / 1440
  143. 	'WScript.Echo "Approx last logon timestamp: " & intLogonTime + #1/1/1601#
  144.  
  145. 	If intLogonTime + #1/1/1601# < Now - intTimeLimit Then
  146. 		strOld = "Over" & intTimeLimit & "-Y"
  147. 		count = count + 1
  148. 	Else
  149. 		strOld = "Over" & intTimeLimit & "-N"
  150. 	End If
  151.  
  152. 	'Wscript.Echo objRecordset.Fields("dnsHostName") & "," & objRecordSet.Fields("operatingSystemVersion") _
  153. 	'	& ",LOGON-" & intLogonTime + #1/1/1601# & "," & strOld
  154. 	objFile.WriteLine objRecordset.Fields("dnsHostName") & "," & objRecordSet.Fields("operatingSystemVersion") _
  155. 		& "," & objRecordSet.Fields("operatingSystem") & ",LOGON-" & intLogonTime + #1/1/1601# & "," & strOld
  156. 	'WScript.Echo " Has not logged on in " & intTimeLimit & " days"
  157. 	'WScript.Echo " Approx last logon timestamp: " & intLogonTime + #1/1/1601#
  158.  
  159. 	'count = count + 1
  160. 	'End If
  161. 	Set objComputer = Nothing
  162. End Function
  163.  

page last uploaded: 2017-08-21, 14:26