Thursday, March 5, 2009

Splitting Log files

Here's how you can split any file into as many multiple parts as you need. This is good for when you need to read very large log files on windows machines.

Option Explicit

'variables for the text files
Dim sourcefile,target1,targetDir,basename,targetfile,strfilenum,filenumber,linenumber

'variables objects
Dim objfs, isnotsplit, WorkingFolder

'initialize arguments
Set objfs = CreateObject("Scripting.FileSystemObject")
Set WorkingFolder = objfs.GetFolder(".\")
sourcefile = WorkingFolder & "\source.txt"
basename = "splitlog"
targetDir = WorkingFolder & "\newfiles\"
filenumber = 1
linenumber = 1
' check the file exists
If objFS.FileExists( sourceFile ) Then
WScript.Echo ("input file exists...")
strfilenum = "" & filenumber & ""
targetfile = targetDir + "\" + basename + "_" + strfilenum + "." + objFS.GetExtensionName(sourceFile)
Set target1 = objFS.CreateTextFile(targetfile, True)
WScript.Echo "Creating file " & filenumber & "."

With objfs.OpenTextFile(sourceFile)

While Not .AtEndOfStream
' new files
If linenumber <>
target1.WriteLine .ReadLine
linenumber = linenumber + 1
Else
target1.WriteLine .ReadLine
target1.close
filenumber = filenumber + 1
strfilenum = "" & filenumber & ""
targetfile = targetDir + "\" + basename + "_" + strfilenum + "." + objFS.GetExtensionName(sourceFile)
Set target1 = objFS.CreateTextFile(targetfile, True)
linenumber = 1
WScript.Echo "Creating file " & filenumber & "."
End If
Wend
.Close
End With
target1.Close
If( strfilenum = 1 ) Then
WScript.Echo ("input file not split(Too Small)...")
else
WScript.Echo ("input file split...")
End If
Else
WScript.Echo ("input file does not exist...")
End if
WScript.Echo ("finished!")
'end

No matter how many resulting parts you need, this script will work. every 60,000 lines it just starts a new file with a new file name. I used this to split a log with over 1.2million lines.(180 meg txt file.)

Wednesday, February 11, 2009

ADO RECORD SETS.

An ado recordset(vbscript) does not iterate when you put it in a do while loop.

You have to tell it to iterate with the command


adoRecordset.MoveNext.


See? That wasn't hard. Too bad it took me 2 freaking hours to find and remember!!!!!!!

Thank you for your time. Have a good day. :)

Friday, February 6, 2009

HOW TO QUERY SUN ONE LDAP WITH VBSCRIPT

I have searched high and I have searched low, but even though all M$ documentation claims that VB script can be used to work on any Version 3 Compliant LDAP Directory, I could not find any one or any documentation on how to do it.

After a time I figured out how to bind to LDAP using the DSObject binding and load the LDAP directory into a collection.

Set objDSO = GetObject(strProvider)
Set objDomain = objDSO.OpenDSObject (LDAP://server1.domain.com/ou=people,dc=domain,dc=com,CN=ADMIN USERID, Password, 0)

This will load the LDAP directory in it's entirety and allow you manipulate any user and make modifications or reports.

The down side is that it's very slow to manipulate and if you need to do a comparision between the LDAP collection and a csv (for example) you may end up reloading the collection several times and it can run very slowly.

Recently I have discovered another way to work with LDAP in VBscript. You can use an ADO search to query ldap and return only the attributes you want to work with. This is much faster than using the OpenDSObject and allows you do queries and comparisions and load the individual users if you need to do modifications.

To do an ADO search

' Use ADO to search Active Directory.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Properties("User ID") = "cn=ADMINID"
adoConnection.Properties("Password") = "PASSWORD"
adoConnection.Properties("Encrypt Password") = False
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

'Search String
strBase = ""

' Filter on all user objects.
strFilter = "(objectClass=person)"

' Comma delimited list of attribute values to retrieve.
strAttributes = "cn,uid"

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"

' Run the query.
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 60
adoCommand.Properties("Cache Results") = False
Set adoRecordset = adoCommand.Execute

' Enumerate resulting recordset.
Do Until adoRecordset.EOF
strUID= adoRecordset.Fields("uid").Value
WScript.Echo strDN(0)
strCN= adoRecordset.Fields("cn").Value
WScript.Echo struid(0)
Loop

There are some oddities I'm still working through. many values in LDAP seem to come up as arrays, rather htan string values, even though there are no other values in the array. Others do not. cn, and uid both came up as single value arrays, but the createtimestamp didn't.

If you need to get a users DN through this query, simply query the attribute, entryDN. yOu can use this to bind to the user and make modifications if needed, just as you would on AD.

One other great thing you can do is use this same code to query Oracle Iternet Directory. Unfortunately, OID does not have an ADSPATH (like AD) or an entryDN(LDAP) attribute so it may not help with searching users for the purposes of modifications, but you can use this method to run reports.

Enjoy.