'Get IP and Hostname of an UPN from Exchange SMTP Log 
'Atuhor Felipe 23/11/2007
'Vesion 1.0

'_____________GLOBAL VARS
dim strScriptFile : strScriptFile = WScript.ScriptFullname
dim sPath : sPath = Left(strScriptFile, Len(strScriptFile) - Len(WScript.Scriptname)) 
Const ForReading=1,ForWriting=2,ForAppending=8,adLockOptimistic = 3,adOpenDynamic = 1
Const adOpenStatic = 3,adUseClient = 3

'______________LOGPARSER
Dim objLogParser : Set objLogParser = CreateObject("MSUtil.LogQuery")
Dim objInputFormat : Set objInputFormat = CreateObject("MSUtil.LogQuery.IISNCSAInputFormat")	
Dim objOutputFormat : Set objOutputFormat = CreateObject("MSUtil.LogQuery.CSVOutputFormat")

'______________EDIT
Dim intDaySpam : intDaySpam = 1   'From how many days Ago
Dim verbose : verbose = true

'_____________SMTP IN (FRONT END)
	Dim serverMCL(3)
	serverMCL(0) = "MAIL01B01"
	serverMCL(1) = "MAIL01B02"
	serverMCL(2) = "MAIL01B03"
	serverMCL(3) = "MAIL01B04"	
	
nomelog = GetFormatedDate(date)
nomelog = "nc" & nomelog & ".log"

'Could get a list of UPNs from a txt, or be implemented into some other scripts
GetSMTPInfo("spirit@domain.com")


Function GetSMTPInfo(strUPN)
'Does the Querying In Remote Servers	
	'Export UPN already in BlackList  to blacklist.txt	
	i= 0
	For i = 0 To UBound(serverMCL)
	if serverMCL(i) <> "" then
		sFile = sPath & "logs\log_" & serverMCL(i) & ".txt" 			
		'strQuery ="select RemoteHostName,Username,Request into " & sFile & " from \\MAIL01B01\D$\SMTPLogs\SMTPSVC1\nc" & nomelog &".log where StatusCode = '250' and Request like '%" & strUPN & "%'"
		strQuery ="select RemoteHostName,Username,Request into " & sFile & " from \\MAIL01B0\D$\SMTPLogs\SMTPSVC1\" & nomelog &" where Request like '%" & strUPN & "%'"
		call DisplayMsg( "Querying : " & serverMCL(i) & " Exchange SMTP IN LogFile : " & nomelog & ", for UPN  " & strUPN )
		Call DisplayMsg(strQuery)
		
		objLogParser.ExecuteBatch strQuery, objInputFormat, objOutputFormat	
		'call ParseResult(sFile,outputfile,"clean")
	end if
	next
end function

Function GetFormatedDate(inputDate)
 Dim intMonth : intMonth = Right("00" & Month(inputDate), 2)
 Dim intDay : intDay = Right("00" & Day(inputDate), 2) 
 intDay = intDay - intDaySpam 
 if len(intDay) = 1 then
 intDay = 0 & intDay  
 end if 
 Dim intYear : intYear = Right("00" & Year(inputDate),2)
 GetFormatedDate = intYear & intMonth & intDay
End Function

Function DisplayMsg(msgTxt)
'Prints msg on screen only if Verbose is set	
	if Verbose = true then		
		wscript.echo msgTXT
	end if
end Function

