使用vbscript从日志文件的最后一行提取文本

时间:2015-10-26 18:55:17

标签: regex for-loop vbscript

我有一个格式化的日志文件:

AssetTag USERNAME日期时间本地

abc123456 Sam 10/15/2015 8:22:14 am Local

abc87363 Joe 10/15/2015 8:55:59 am local

我需要使用批处理文件从日志文件的最后一行提取USERNAME。 IE:从最后一行提取'Joe'。

我正在考虑使用某种正则表达式找到第一个空格并选择第一个空格和下一个空格之间的文本......应该是'Joe'......

我用它来提取最后一行:

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\logs\UserAudit.log", ForReading)

Do Until objFile.AtEndOfStream
    strNextLine = objFile.ReadLine
    If Len(strNextLine) > 0 Then
        strLine = strNextLine
    End If
Loop

objFile.Close

Wscript.Echo strLine

编辑:实际上是2个标签分隔资产标签和用户名,1个标签分隔用户名和日期

1 个答案:

答案 0 :(得分:0)

我想了解更多google ...

以下脚本的作用..

1)提示资产标签 2)从日志文件中读取最后一行,提取用户名 3)在AD中查找用户名以查找员工姓名

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
strAsset= INPUTBOX("Please enter the Asset Tag:")
strLog = "\\" & strAsset & "\C$\Logs\UserAudit.log"

'Set objFile = objFSO.OpenTextFile("C:\logs\UserAudit.log", ForReading)
Set objFile = objFSO.OpenTextFile(strLog, ForReading)

Do Until objFile.AtEndOfStream
    strNextLine = objFile.ReadLine
    If Len(strNextLine) > 0 Then
        strLine = strNextLine
    End If
Loop

objFile.Close
intStart = InStr(strLine, "     ")
If intStart <> 0 Then
        intStart = intStart + 2
        strText = Mid(strLine, intStart, 250)
        For i = 1 to Len(strText)
            If Mid(strText, i, 1) = "   " Then
                Exit For
            Else
                strData = strData & Mid(strText, i, 1)
            End If
        Next
    End If

'Username from Log file
Wscript.Echo strData

'Lookup Account for Staff Name

SET objSystemInfo = CREATEOBJECT("ADSystemInfo") 
strDomain = objSystemInfo.DomainShortName
strUser = strData

wscript.echo GetUserDN(strUser,strDomain)
strAD = GetUserDN(strUser,strDomain)

intStart = InStr(strAD, "CN=")
If intStart <> 0 Then
        intStart = intStart + 3
        strText = Mid(strAD, intStart, 250)
        For i = 1 to Len(strText)
            If Mid(strText, i, 3) = ",OU" Then
                Exit For
            Else
                strName = strName & Mid(strText, i, 1)
            End If
        Next
    End If
wScript.echo strName

FUNCTION GetUserDN(BYVAL strUserName,BYVAL strDomain)
    SET objTrans = CREATEOBJECT("NameTranslate")
    objTrans.Init 1, strDomain
    objTrans.SET 3, strDomain & "\" & strUserName 
    strUserDN = objTrans.GET(1) 
    GetUserDN = strUserDN
END FUNCTION

'Cleanup
intStart=""
strUser=""
strDomain=""
strName=""
strAD=""
strText=""
strLine=""
strNextLine=""