Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to process a Range 1

Status
Not open for further replies.

djtech2k

MIS
Jul 24, 2003
1,097
US
I am in need of some help here. I think I have a concept, but am not sure how to accomplish it.

I need to write a script to lookup some information in AD. I can handle that part fine. The part I need help with si that i need it to lookup computernames that have a certain range of barcodes in the name. I know the format and how to query AD for it, but I need help on the way I can have it dom some sort of "for each next" so that I can start with a barcode and do its thing, then go to the next barcode and try. I am assuming that I may need to use an array or some type of loop, but I am not sure how I could use a loop here since I am not dealing with a collection of items. I thought that an array could be that collection, but my array skills are very limited.

Any help is appreciated.
 
A starting point:
For Each strPC In Array("computer1", "computer2", "computer3")
MsgBox strPC
Next

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
How would I assign the values to the array though? I know the start and ending barcode, but I do not know how to assign the values. I think I can pull the data from the array and definitely do the AD query.
 
For more detail:

I have a script I am using to identify old computers in my domain. I want to make a new version of this script so that it will only list computers that are old that fall into a certain barcode range(s).

For example, my naming convention would be HELPxxxxxx ("x" being a barcode). I want my script to just report computers that are between "100000" and "110982". I can handle adding/removing the prefix and querying AD. I just need a machanism to enumerate the barcodes incrementally by 1's until the end of the range.
 
If you have your AD query then just pull the barcode part of the name and compare it.

Is it greater than or equal to 100000 and less than or equal to 110982.

Another possible way would be to add the possible values into a dictionary and use the Exists method.

Code:
Dim strComputer : strComputer = "hElp109854"
Dim objDict : Set objDict = CreateObject("Scripting.Dictionary")

For i = 100000 To 110982
	objDict.Add "HELP" & i, ""
Next

If objDict.Exists(UCase(strComputer)) Then
	WScript.Echo strComputer & " is an older computer!"
End If

Another one might be a Regular Expression with the right pattern.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Here is what I am using, but for some reason it keeps echoing out the same cn repeatedly. My eyes have crossed from looking at this, but it appears that I am not making it to the "next" in the "For Loop". Maybe I am way off base with it, but I was taking a shot:

Code:
Option Explicit

Dim objShell, lngBiasKey, lngBias, objRootDSE
Dim strDNSDomain, objCommand, objConnection
Dim strComputer, strBase, strFilter, strAttributes
Dim strQuery, objRecordSet, strCN, strPWD, strLL
Dim dtPWD, dtLL, objDate, objDate2, i

' Obtain local time zone bias from machine registry.
Set objShell = CreateObject("Wscript.Shell") lngBiasKey = objShell.RegRead("HKLM\System\CurrentControlSet\Control\" _
  & "TimeZoneInformation\ActiveTimeBias")
If UCase(TypeName(lngBiasKey)) = "LONG" Then
  lngBias = lngBiasKey
ElseIf UCase(TypeName(lngBiasKey)) = "VARIANT()" Then
  lngBias = 0
  For k = 0 To UBound(lngBiasKey)
    lngBias = lngBias + (lngBiasKey(k) * 256^k)
  Next
End If

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

' Use ADO to search Active Directory.
Set objCommand = CreateObject("ADODB.Command") Set objConnection = CreateObject("ADODB.Connection") 
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection

On Error Resume Next

For i = 507508 To 568608
strComputer = "PREFIX" & i & "$"
'wscript.echo strComputer & " START"

strBase = "<LDAP://" & strDNSDomain & ">"
strFilter = "(&(objectCategory=computer)(objectClass=computer)(sAMAccountName=" & strComputer & "))"
'wscript.echo strComputer & " AGAIN"
strAttributes = "cn,pwdLastSet,lastLogonTimeStamp"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False 
Set objRecordSet = objCommand.Execute
  
    Do Until objRecordSet.EOF
  strCN = objRecordSet.Fields("cn")
  strPWD = objRecordSet.Fields("pwdLastSet")
  strLL = objRecordSet.Fields("lastLogonTimeStamp")
  Set objDate = strPWD
  dtPWD = Integer8Date(objDate, lngBias)

  Set objDate2 = strLL
  dtLL = Integer8Date2(objDate2, lngBias)

  Wscript.Echo strCN & " ; " & dtPwd & " ; " & dtLL
  	
    objRecordSet.MoveNext
	Loop

objConnection.Close
strComputer = ""
Next


Function Integer8Date(objDate, lngBias)
' Function to convert Integer8 (64-bit) value to a date, adjusted for ' time zone bias.
 Dim lngAdjust, lngDate, lngHigh, lngLow
  lngAdjust = lngBias
  lngHigh = objDate.HighPart
  lngLow = objDate.LowPart
' Account for bug in IADsLargeInteger property methods.
  If (lngHigh = 0) And (lngLow = 0) Then
    lngAdjust = 0
  End If
  lngDate = #1/1/1601# + (((lngHigh * (2 ^ 32)) _
    + lngLow) / 600000000 - lngAdjust) / 1440
  Integer8Date = CDate(lngDate)
End Function

Function Integer8Date2(objDate2, lngBias) 
' Function to convert Integer8 (64-bit) value to a date, adjusted for ' time zone bias.
Dim lngAdjust, lngDate, lngHigh, lngLow
  lngAdjust = lngBias
  lngHigh = objDate2.HighPart
  lngLow = objDate2.LowPart
' Account for bug in IADsLargeInteger property methods.
  If (lngHigh = 0) And (lngLow = 0) Then
    lngAdjust = 0
  End If
  lngDate = #1/1/1601# + (((lngHigh * (2 ^ 32)) _
    + lngLow) / 600000000 - lngAdjust) / 1440
  Integer8Date2 = CDate(lngDate)
End Function
 
I'd move objConnection.Close after the Next:
...
objRecordSet.MoveNext
Loop
strComputer = ""
Next
objConnection.Close

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top