Solved

# DHCP Scripr error

Posted on 2007-03-25
513 Views
Hi,

I have a DHCP vb script but gives an error can any one help me in this.

'**** DHCP Server Log Checking Script
'**** You may use this script free of charge but may not make any changes except to the variables to meet your needs
'**** without the permission of the author.
'****
'****
'**** The script is best set-up to run as a scheduled task. It can check for any event ID in the DHCP Server logs.
'**** It does not need to run on the DHCP server but you will need the full UNC path and the user that the script
'**** is set to run as will need rights to the servers log file areas.
'****
'**** The time interval is configurable and your scheduled task running time should be set to match that so areas of the
'**** logs don't go unchecked.

'Variables

strfindtime = dateadd("n", -30, Time)           ' Change this figure (-30) to set interval between checking (in minutes)
intfindtime = 30 ' Change this value to match the one above
strtoday = (WeekdayName(Weekday(Date),true))
strlogfile = "\\inexchange1\c$\WINDOWS\system32\dhcp\dhcpsrvlog." & strtoday ' Change the locations to match that of your DHCP Server streventid = "10" ' Change to meet what event you want to check for - See the beginning of a log to see a definition list of the Event ID's strTo = "sharath.reddy@***.com" ' E-mail address TO strSubject = "DHCP Server Alert" ' E-mail Subject objCDOMail.From = "sharath.reddy@***.com" 'E-mail from address (needs to be a valid address) objCDOMail.From = "SYSTEM <INEXCHANGE1.***.co.uk>" ' E-mail from address (needs to be a valid address) 'Leave the rest of the script Dim strTo, strSubject, strBody, i Dim objCDOMail set objCDOMail = CreateObject("CDONTS.NewMail") const forreading = 1 set objfso = createobject("Scripting.filesystemobject") set objtextfile = objfso.opentextfile(strlogfile, forreading) do while objtextfile.atendofstream <> True ceventid = "" posfind = 0 strline = objtextfile.readline if instr(strline, "ID Date,Time,Description,IP Address,Host Name,MAC Address") then strline = objtextfile.readline else end if if instr(strline, ",") then arrdhcprecord = split(strline, ",", 7) ceventid = arrdhcprecord(0) ceventdte = arrdhcprecord(1) ceventtime = arrdhcprecord(2) ceventdesc = arrdhcprecord(3) ceventip = arrdhcprecord(4) ceventhost = arrdhcprecord(5) ceventmac = arrdhcprecord(6) else End if i=i+1 findtime = datediff("n", ceventtime, strfindtime) if findtime < intfindtime then posfind = posfind + 1 else end if if ceventid = streventid then posfind = posfind + 1 else end if if posfind = 2 then strBody = "===================" & vbCrLf strBody = strBody & " DHCP Server Alert" & vbCrLf strBody = strBody & "===================" & vbCrLf strBody = strBody & vbCrLf strBody = strBody & "Event ID: " & ceventid & vbCrLf strBody = strBody & "Date: " & ceventdte & vbCrLf strBody = strBody & "Time: " & ceventtime & vbCrLf strBody = strBody & "Desc: " & ceventdesc & vbCrLf strBody = strBody & "IP: " & ceventip & vbCrLf strBody = strBody & "Host: " & ceventhost & vbCrLf strBody = strBody & "Mac:" & ceventmac & vbCrLf objCDOMail.To = strTo objCDOMail.Subject = strSubject objCDOMail.Body = strBody objCDOMail.Send else end if loop objtextfile.close Set objCDOMail = Nothing 0 Question by:bsharath • 14 • 11 25 Comments LVL 8 Expert Comment ID: 18790883 Please tell us what is the error you are getting while executing this script 0 LVL 11 Author Comment ID: 18790998 --------------------------- Windows Script Host --------------------------- Script: C:\Documents and Settings\sharathr\Desktop\dhcpchecker_287\dhcpchecker.vbs Line: 23 Char: 1 Error: Object required: '' Code: 800A01A8 Source: Microsoft VBScript runtime error --------------------------- OK --------------------------- 0 LVL 8 Expert Comment ID: 18791088 Create another variable on line 23 and 24; Line 23, and 24 to after line 30 where you have written "set objCDOMail = CreateObject("CDONTS.NewMail")" this will help this error was because you have tried to use the object "objCDOMail" before initializing it. 0 LVL 8 Expert Comment ID: 18791092 you can create variable to define to and from email address. use these variables in the form. objCDOMail.From = variable1 objCDOMail.From = variable2 0 LVL 8 Expert Comment ID: 18791102 change this portion of script 'Variables estrfindtime = dateadd("n", -30, Time) ' Change this figure (-30) to set interval between checking (in minutes) intfindtime = 30 ' Change this value to match the one above strtoday = (WeekdayName(Weekday(Date),true)) strlogfile = "\\inexchange1\c$\WINDOWS\system32\dhcp\dhcpsrvlog." & strtoday    ' Change the locations to match that of your DHCP Server
streventid = "10"    ' Change to meet what event you want to check for - See the beginning of a log to see a definition list of the Event ID's
strTo = "sharath.reddy@***.com" ' E-mail address TO
strSubject = "DHCP Server Alert" ' E-mail Subject
objCDOMail.From = "sharath.reddy@***.com" 'E-mail from address (needs to be a valid address)
objCDOMail.From    =  "SYSTEM <INEXCHANGE1.***.co.uk>" ' E-mail from address (needs to be a valid address)

'Leave the rest of the script

Dim strTo, strSubject, strBody, i
Dim objCDOMail
set objCDOMail = CreateObject("CDONTS.NewMail")
set objfso = createobject("Scripting.filesystemobject")
do while objtextfile.atendofstream <> True
ceventid = ""
posfind = 0

to

'Variables

strfindtime = dateadd("n", -30, Time)           ' Change this figure (-30) to set interval between checking (in minutes)
intfindtime = 30 ' Change this value to match the one above
strtoday = (WeekdayName(Weekday(Date),true))
strlogfile = "\\inexchange1\c$\WINDOWS\system32\dhcp\dhcpsrvlog." & strtoday ' Change the locations to match that of your DHCP Server streventid = "10" ' Change to meet what event you want to check for - See the beginning of a log to see a definition list of the Event ID's strTo = "sharath.reddy@***.com" ' E-mail address TO strSubject = "DHCP Server Alert" ' E-mail Subject 'Leave the rest of the script Dim strTo, strSubject, strBody, i Dim objCDOMail set objCDOMail = CreateObject("CDONTS.NewMail") objCDOMail.From = strTo 'you have already defined the email-to field objCDOMail.From = "SYSTEM <INEXCHANGE1.***.co.uk>" ' E-mail from address (neds to be a valid address) const forreading = 1 set objfso = createobject("Scripting.filesystemobject") set objtextfile = objfso.opentextfile(strlogfile, forreading) do while objtextfile.atendofstream <> True ceventid = "" posfind = 0 this should work :) 0 LVL 11 Author Comment ID: 18791722 mailtosinghs: Now i get this error C:\>cscript a.vbs Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001. All rights reserved. C:\a.vbs(88, 3) (null): 0x80040108 0 LVL 8 Expert Comment ID: 18791781 this error is related to CDONTS can you tell me what is on 88th line in that file... 0 LVL 11 Author Comment ID: 18791813 This is from the first line strBody = strBody & "===================" & vbCrLf Or started count from 'Variables if posfind = 2 then strBody = "===================" & vbCrLf strBody = strBody & " DHCP Server Alert" & vbCrLf strBody = strBody & "===================" & vbCrLf strBody = strBody & vbCrLf strBody = strBody & "Event ID: " & ceventid & vbCrLf strBody = strBody & "Date: " & ceventdte & vbCrLf strBody = strBody & "Time: " & ceventtime & vbCrLf strBody = strBody & "Desc: " & ceventdesc & vbCrLf strBody = strBody & "IP: " & ceventip & vbCrLf strBody = strBody & "Host: " & ceventhost & vbCrLf strBody = strBody & "Mac:" & ceventmac & vbCrLf objCDOMail.To = strTo objCDOMail.Subject = strSubject objCDOMail.Body = strBody objCDOMail.Send else Else is on the 80th line 0 LVL 8 Expert Comment ID: 18791835 please mail me this in .txt attachment to my user name here at yahoo dot com 0 LVL 8 Expert Comment ID: 18791846 I dont know whether posting email address for attachments are allowed or not; alternatively you can paste the entire script here again 0 LVL 11 Author Comment ID: 18792031 Here is the full script '**** DHCP Server Log Checking Script '**** Copyright © 2005 Chris Pratt '**** You may use this script free of charge but may not make any changes except to the variables to meet your needs '**** without the permission of the author. '**** '**** '**** The script is best set-up to run as a scheduled task. It can check for any event ID in the DHCP Server logs. '**** It does not need to run on the DHCP server but you will need the full UNC path and the user that the script '**** is set to run as will need rights to the servers log file areas. '**** '**** The time interval is configurable and your scheduled task running time should be set to match that so areas of the '**** logs don't go unchecked. 'Variables strfindtime = dateadd("n", -30, Time) ' Change this figure (-30) to set interval between checking (in minutes) intfindtime = 30 ' Change this value to match the one above strtoday = (WeekdayName(Weekday(Date),true)) strlogfile = "\\in-ads-srv02\c$\WINNT\system32\dhcp\DhcpSrvLog." & strtoday    ' Change the locations to match that of your DHCP Server
streventid = "10"    ' Change to meet what event you want to check for - See the beginning of a log to see a definition list of the Event ID's
strTo = "sharath.reddy@" ' E-mail address TO
strSubject = "DHCP Server Alert" ' E-mail Subject

'Leave the rest of the script

Dim strTo, strSubject, strBody, i
Dim objCDOMail
set objCDOMail = CreateObject("CDONTS.NewMail")
objCDOMail.From = strTo 'you have already defined the email-to field
objCDOMail.From    =  "SYSTEM <sharath.reddy@com>" ' E-mail from address (neds to be a valid address)
set objfso = createobject("Scripting.filesystemobject")
do while objtextfile.atendofstream <> True
ceventid = ""
posfind = 0

else

end if

if instr(strline, ",") then
arrdhcprecord = split(strline, ",", 7)
ceventid = arrdhcprecord(0)
ceventdte = arrdhcprecord(1)
ceventtime = arrdhcprecord(2)
ceventdesc = arrdhcprecord(3)
ceventip = arrdhcprecord(4)
ceventhost = arrdhcprecord(5)
ceventmac = arrdhcprecord(6)
else

End if
i=i+1

findtime = datediff("n", ceventtime, strfindtime)

if findtime < intfindtime then
posfind = posfind + 1
else

end if

if ceventid = streventid then
posfind = posfind + 1
else

end if

if posfind = 2 then
strBody = "===================" & vbCrLf
strBody = strBody & " DHCP Server Alert" & vbCrLf
strBody = strBody & "===================" & vbCrLf
strBody = strBody & vbCrLf
strBody = strBody & "Event ID: " & ceventid & vbCrLf
strBody = strBody & "Date: " & ceventdte & vbCrLf
strBody = strBody & "Time: " & ceventtime & vbCrLf
strBody = strBody & "Desc: " & ceventdesc & vbCrLf
strBody = strBody & "IP: " & ceventip & vbCrLf
strBody = strBody & "Host: " & ceventhost & vbCrLf
strBody = strBody & "Mac:" & ceventmac & vbCrLf
objCDOMail.To      = strTo
objCDOMail.Subject = strSubject
objCDOMail.Body    = strBody
objCDOMail.Send
else

end if

loop
objtextfile.close
Set objCDOMail = Nothing
0

LVL 8

Expert Comment

ID: 18792090
Use the modified script. I have moved set objcdomail.... to inside the while loop

'**** DHCP Server Log Checking Script
'**** You may use this script free of charge but may not make any changes except to the variables to meet your needs
'**** without the permission of the author.
'****
'****
'**** The script is best set-up to run as a scheduled task. It can check for any event ID in the DHCP Server logs.
'**** It does not need to run on the DHCP server but you will need the full UNC path and the user that the script
'**** is set to run as will need rights to the servers log file areas.
'****
'**** The time interval is configurable and your scheduled task running time should be set to match that so areas of the
'**** logs don't go unchecked.
'Variables

strfindtime = dateadd("n", -30, Time)           ' Change this figure (-30) to set interval between checking (in minutes)
intfindtime = 30 ' Change this value to match the one above
strtoday = (WeekdayName(Weekday(Date),true))
strlogfile = "\\in-ads-srv02\c$\WINNT\system32\dhcp\DhcpSrvLog." & strtoday ' Change the locations to match that of your DHCP Server streventid = "10" ' Change to meet what event you want to check for - See the beginning of a log to see a definition list of the Event ID's strTo = "sharath.reddy@isc.com" ' E-mail address TO strSubject = "DHCP Server Alert" ' E-mail Subject 'Leave the rest of the script Dim strTo, strSubject, strBody, i Dim objCDOMail const forreading = 1 set objfso = createobject("Scripting.filesystemobject") set objtextfile = objfso.opentextfile(strlogfile, forreading) do while objtextfile.atendofstream <> True ceventid = "" posfind = 0 strline = objtextfile.readline if instr(strline, "ID Date,Time,Description,IP Address,Host Name,MAC Address") then strline = objtextfile.readline else end if if instr(strline, ",") then arrdhcprecord = split(strline, ",", 7) ceventid = arrdhcprecord(0) ceventdte = arrdhcprecord(1) ceventtime = arrdhcprecord(2) ceventdesc = arrdhcprecord(3) ceventip = arrdhcprecord(4) ceventhost = arrdhcprecord(5) ceventmac = arrdhcprecord(6) else End if i=i+1 findtime = datediff("n", ceventtime, strfindtime) if findtime < intfindtime then posfind = posfind + 1 else end if if ceventid = streventid then posfind = posfind + 1 else end if if posfind = 2 then strBody = "===================" & vbCrLf strBody = strBody & " DHCP Server Alert" & vbCrLf strBody = strBody & "===================" & vbCrLf strBody = strBody & vbCrLf strBody = strBody & "Event ID: " & ceventid & vbCrLf strBody = strBody & "Date: " & ceventdte & vbCrLf strBody = strBody & "Time: " & ceventtime & vbCrLf strBody = strBody & "Desc: " & ceventdesc & vbCrLf strBody = strBody & "IP: " & ceventip & vbCrLf strBody = strBody & "Host: " & ceventhost & vbCrLf strBody = strBody & "Mac:" & ceventmac & vbCrLf set objCDOMail = CreateObject("CDONTS.NewMail") objCDOMail.From = "SYSTEM <sharath.reddy@i.com>" ' E-mail from address (neds to be a valid address) objCDOMail.To = strTo objCDOMail.Subject = strSubject objCDOMail.Body = strBody objCDOMail.Send Set objCDOMail = Nothing else end if loop objtextfile.close 0 LVL 11 Author Comment ID: 18792174 Hi Some thing went on it came out without any error but how do i find it is working. Until you have understood can you tell me what this script does THX Sharath 0 LVL 8 Expert Comment ID: 18792216 if you wnat to know that it went into the loop or not you can insert the line WScript.Echo "some text" and run the script again, you should see the echo message while the script in loop. if this was not what you need, please specify the qn again clearly 0 LVL 8 Expert Comment ID: 18792226 please insert above line before the objCDOMail.Send 0 LVL 11 Author Comment ID: 18792322 I got a box containing "Sometext" But where will i get the result.I now that this is correct but where can i see the result. THX For replying so patiently. THX Sharath 0 LVL 8 Expert Comment ID: 18792365 You will get the result in the email address mentioned in the script which is sharath.reddy@isc.com here. For a successful email, you need direct internet connectivity of the system where this script is running, or you have to setup intranet properly 0 LVL 11 Author Comment ID: 18792432 This mail will come when ever a error is encountered on the server or ?? Is there any wany i can genetrate a faulse error or through the script can we send a test mail. 0 LVL 8 Expert Comment ID: 18792560 this will send a mail if you have eventid 10 in today's dhcp log in past 30 min. if you want to generate an email do this:..... open \\in-ads-srv02\c$\WINNT\system32\dhcp\DhcpSrvLog." & strtoday
for this go to the folder "\\in-ads-srv02\c\$\WINNT\system32\dhcp\"
open today's logfile dhcpsrvlog.mon if day is monday
you will find following texts after scrolling some lines

00,04/19/99,12:43:06,Started,,,
60,04/19/99,12:43:21,No DC is DS Enabled,,MYDOMAIN,
63,04/19/99,12:43:28,Restarting rogue detection,,,
01,04/19/99,13:11:13,Stopped,,,
00,04/19/99,12:43:06,Started,,,
55,04/19/99,12:43:54,Authorized(servicing),,MYDOMAIN,

change 10 in following line of code to 00 in the script file.
streventid = "10"

stop and restart the dhcp service.
rerun the script

WScript.Echo "some text"
or change this to a comment
0

LVL 8

Expert Comment

ID: 18792564
the log text i have posted is a sample, you may find different texts but in same format
0

LVL 11

Author Comment

ID: 18792772
In the log file what should i change
0

LVL 11

Author Comment

ID: 18792798
I have changed all 10 to 00 the restart the DHCP service the ran the script but did not receive any mails

THX
Sharath
0

LVL 8

Accepted Solution

mailtosinghs earned 500 total points
ID: 18792902
CDONTS is not able to send mail to the the recipient.

CDONTS is older version and not recommended. CDOSYS is now used in place of CDONTS and is more reliable and configurable.
you should use CDOSYS to send mail using an SMTP server in your intranet where the ip of your system (where the script is executing) is  allowed to relay mails.

for cdosys example search cdosys examples in google.

http://kb.webecs.com/Article.aspx?id=10169&cNode=7U0G0Q
http://www.pacificonline.com/SmarterTicket/Customer/KBArticle.aspx?articleid=36&KBSearchID=16275
0

LVL 11

Author Comment

ID: 18792937
Hi,

I have no clue on what you said now sorry to say i dont know anything about scripting.Please help me in this regard.

THX
Sharath
0

LVL 11

Author Comment

ID: 18890149
Thanks for the support.Even though i havent got the solution i am closing the question
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Citrix XenApp and Microsoft CALs 6 75
Unable to display time in the powershell script parsing Event Log ? 4 54
server is not seen in network 12 72
ost file to pst 10 105
Welcome to my series of short tips on migrations. Whilst based on Microsoft migrations the same principles can be applied to any type of migration. My first tip is around source server preparation. No migration is an easy migration, there is a…
Remote Apps is a feature in server 2008 which allows users to run applications off Remote Desktop Servers without having to log into them to run the applications.  The user can either have a desktop shortcut installed or go through the web portal to…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …