'
' SMTP Settings Checker.
'
' Just run this on a server and all server info it can find will be done. 
' 
' Best run with cscript, or you will have lots of OK boxen to click.
'

'
' Array of stuff to check, these are used for the registry and file checks.
'
'
Dim ServerStrings(11)
ServerStrings(0) = "qah-mail1"
ServerStrings(1) = "195.188.238.26"
ServerStrings(2) = "mci-mail1"
ServerStrings(3) = "193.130.225.55"
ServerStrings(4) = "sun-ldp-mal-str-dr1"
ServerStrings(5) = "83.137.209.15"
ServerStrings(6) = "sun-arc-mal-sd1"
ServerStrings(7) = "195.188.239.42"
ServerStrings(8) = "sun-mal-l1"
ServerStrings(9) = "195.188.239.14"
ServerStrings(10) = "jobs-mail-l1"
ServerStrings(11) = "195.188.238.134"

StartTime = Timer
ChecksCount = 0
InstanceCount = 0 
FileCount = 0

Set logFileObject = CreateObject("Scripting.FileSystemObject")
Set logFile = logFileObject.CreateTextFile("C:\smtp-checker.log", True)

Set WshNetwork = CreateObject("WScript.Network")
strLocalServer = WshNetwork.ComputerName

WScript.Echo "Server: " & strLocalServer
logfile.writeline "Server: " & strLocalServer

Set WindowsVersion = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strLocalServer & "\root\cimv2")

Set colOperatingSystems = WindowsVersion.ExecQuery _
("Select * from Win32_OperatingSystem")

For Each objOperatingSystem in colOperatingSystems

If InStr (1,objOperatingSystem.Caption, "2000",1) Then
	SMTP2000
	SQLCheck
	CheckRegistry
	CheckFiles
	Else 
	SMTPServerCheck
	SQLCheck
	CheckRegistry
	CheckFiles
End If 

Next

wscript.echo "Checks completed. Time taken : " & Timer - StartTime & " seconds."
logfile.writeline "Checks completed. Time taken : " & Timer - StartTime & " seconds."
logfile.close

Sub SMTPServerCheck

On Error Resume Next
IISSMTP = 0
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Set objWMIService = GetObject("winmgmts:\\" & strLocalServer & "\root\MicrosoftIISv2")
If err.number <> 0 Then
	wscript.echo "IIS not installed"
	logfile.writeline "IIS not installed"
	On Error GoTo 0
	Exit Sub
End If
Set colItems = objWMIService.ExecQuery("SELECT * FROM IIsSmtpServerSetting", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)
            
For Each objItem In colItems
	WScript.Echo "SMTP Server Installed"
	logfile.writeline "SMTP Server Installed"
  		If objItem.smarthost = "" Then
   			wscript.echo "SMTP Server not using a smarthost."
   			logfile.writeline "SMTP Server not using a smarthost."
   		Else       
      		WScript.Echo "Relays Via: " & objItem.SmartHost
      		logfile.writeline "Relays Via: " & objItem.SmartHost
        	IISSMTP = IISSMTP + 1        
    	End If
Next

If IISSMTP = 0 Then
wscript.echo "IIS installed but no SMTP Server."
logfile.writeline "IIS installed but no SMTP Server."
End If

On Error GoTo 0

End Sub

Sub sqlcheck

FoundSQL = 0

Set db = CreateObject("ADODB.Connection")

cnstring = "Provider=SQLOLEDB.1;User ID=sa;Password=D4t4S0" & Chr(33) & Chr(36) & ";Persist Security Info=False;Initial Catalog=Master;Data Source=127.0.0.1"
db.connectiontimeout = 5
On Error Resume Next
db.open cnstring
If err.number <> 0 Then
	wscript.echo "No SQL server responding. " & err.description
	logfile.writeline "No SQL server responding. " & err.description
	foundsql = foundsql + 1
Else
	sqlstring = "select servername from msdb.dbo.sysmail_server"
	Set rs = db.execute (sqlstring)
	wscript.echo "SQL Server using: " & rs("servername")
	logfile.writeline "SQL Server using: " & rs("servername")
	foundSQL = foundsql + 1
End If

On Error GoTo 0

If foundsql = 0 Then
	wscript.echo "No SQL server responding. NB SQL 2000 has no mail capability."
	logfile.writeline "No SQL server responding. NB SQL 2000 has no mail capability."
End If

End Sub


Sub CheckFiles

wscript.echo "Checking files. This will take a while...."
logfile.writeline "Checking files. This will take a while...."

Dim arrExtensions
arrExtensions = Array("'txt'","'config'","'bat'","'cmd'","'asp'","'aspx'","'asmx'","'html'","'htm'","'cfm'","'ini'","'resx'","'vbs'","'php'","'ascx'","'cs'","'xml'","'xslt'","'sql'","'shtml'","'shtm'","'pl'","'cgi'","'js'")
'arrExtensions = Array("'config'") 

Dim strQueryFilter
strQueryFilter = "Extension = " & Join(arrExtensions, " OR Extension = ")

Dim strComputer
strComputer = "."

Dim objWMIService
Set objWMIService = GetObject("winmgmts:\\" & _
                      strComputer & "\root\cimv2")

Dim colFiles
Set colFiles = objWMIService.ExecQuery("SELECT * FROM CIM_DataFile WHERE " & strQueryFilter,, 48)

Dim objFile
For Each objFile In colFiles
	
	If InStr(1,objfile.name,"smtp-checker.vbs",1) = 0 Then
	
	FileCount = FileCount + 1
	'WScript.Echo "Searching " & objFile.Name
	
	CheckContent objfile.name
	
	End If
	
Next
wscript.echo InstanceCount & " matches for mail servers found in " & FileCount & " searched files. Performed " & ChecksCount & " checks."
logfile.writeline InstanceCount & " matches for mail servers found in " & FileCount & " searched files. Performed " & ChecksCount & " checks."
End Sub

Sub CheckRegistry
'
' counter for registryness
'
iCnt = 0
wscript.echo "Searching Registry for known mail servers."
logfile.writeline "Searching Registry for known mail servers."

'collect regfile.

Dim oWS
Set oWS = CreateObject("WScript.Shell")

Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")

sRegTmp = oWS.Environment("Process")("Temp") & "\RegTmp.tmp "

oWS.Run "regedit /e /a " & sRegTmp, , True '/a enables export as Ansi for WinXP

'wscript.echo Now & " Exported Registry."

With oFSO.GetFile(sRegTmp)
  aRegFileLines = Split(.OpenAsTextStream(1, 0).Read(.Size), vbcrlf)
End With

'wscript.echo Now & " Loaded Registry."

oFSO.DeleteFile(sRegTmp)

  For Each eRegLine in aRegFileLines
    If InStr(1, eRegLine, "[", 1) > 0 Then sRegKey = eRegLine
    '
    ' loop on search array.
    '
    For each sSearchFor in ServerStrings
    	If InStr(1, eRegLine, sSearchFor, 1) >  0 Then
      		If sRegKey <> eRegLine Then
      			wscript.echo (vbcrlf & sRegKey) & vbcrlf & eRegLine
      			logfile.writeline (vbcrlf & sRegKey) & vbcrlf & eRegLine
      		Else
      			wscript.echo (vbcrlf & sRegKey)
      			logfile.writeline (vbcrlf & sRegKey)
		    End If
     	 iCnt = iCnt + 1
    	End If
	Next
  Next

wscript.echo iCnt & " registry keys found."
logfile.writeline iCnt & " registry keys found."

End Sub

Sub SMTP2000

On Error Resume Next
Set objIIS = GetObject _
    ("IIS://" & strLocalServer & "/smtpsvc/1")
If err.number <> 0 Then
 wscript.echo "No Local SMTP Server"
 logfile.writeline "No Local SMTP Server"
 Exit Sub
End if
On Error GoTo 0

If objiis.smarthost = "" Then
	wscript.echo "Local SMTP Server not using a Smarthost."
	logfile.writeline "Local SMTP Server not using a Smarthost."
Else 
	wscript.echo "Relays Via: " & objiis.smarthost
	logfile.writeline "Relays Via: " & objiis.smarthost
End If


End Sub


Sub CheckContent(InputFile)

LineCount = 0
Dim FileToCheck,FileToCheckText

Set FileToCheck = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set FileToCheckText = FileToCheck.OpenTextFile(InputFile, 1)
If err.number <> 0 Then		
	wscript.echo "Failed to open the file: " & InputFile & " " & err.description
	logfile.writeline "Failed to open the file: " & InputFile & " " & err.description
	Exit Sub
End If

On Error GoTo 0

Do While FileToCheckText.AtEndOfStream <> True
LinetoCheck = FileToCheckText.Readline
LineCount = LineCount + 1

	For Each FindMe in ServerStrings
	    ChecksCount = ChecksCount + 1
		If InStr (1, LinetoCheck, FindMe, 1) Then
		wscript.echo FindMe & " found in " & InputFile & " on line: " & LineCount
		logfile.writeline FindMe & " found in " & InputFile & " on line: " & LineCount
		InstanceCount = InstanceCount + 1
		End If			
	Next

Loop
	
FileToCheckText.close

End Sub