Solved

Script that finds newly created objects in AD. Need to email only if a change found.

Posted on 2010-08-26
2
305 Views
Last Modified: 2012-05-10
Hi,

Script that finds newly created objects in AD. Need to email only if a change found.
If nothing is created that day then do not email.

Regards
Sharath
const SCHEDULER_TASK_NAME = "PcsEnumTask"

const SCHEDULER_TASK_START_TIME = "12:00:00"

const ROOT_OU = "ou=Ch,OU=Offices,"



dim LogResult,args

Dim objShell : Set objShell = CreateObject("WScript.Shell")

Dim strOutput : strOutput = objShell.Exec("schtasks /query /fo list").StdOut.ReadAll



if InStr(data, SCHEDULER_TASK_NAME) = 0 then

        'WScript.Echo SCHEDULER_TASK_NAME & " found.."

end if



if InStr(strOutput, SCHEDULER_TASK_NAME) = 0 then

''        WScript.Echo "Create task scheduler [" & SCHEDULER_TASK_NAME & "]..."

        'create task scheduler

        args = "schtasks /Create /F /SC DAILY /TN " & SCHEDULER_TASK_NAME & " /TR """ & Wscript.ScriptFullName & """ /ST " & SCHEDULER_TASK_START_TIME

		'WScript.Echo "args: " & args

		objShell.Run args, 1, True



        'WScript.Echo "Task scheduler [" & SCHEDULER_TASK_NAME & "] created successfully"

else

        'numertae pcs and email

        EnumPcs

		EnumGroups

		EnumUsers

        EmailResult

end if



Sub EmailResult

dim ToAddress,MessageSubject,MessageBody



ToAddress = "Shar@plc.com"





	MessageSubject = "Newly created objects today..."



MessageBody = LogResult 



	Set ol = WScript.CreateObject("Outlook.Application")

	Set ns = ol.getNamespace("MAPI")

	ns.logon "","",true,false

	Set newMail = ol.CreateItem(olMailItem)

	newMail.Subject = MessageSubject

	newMail.Body = MessageBody & vbCrLf



	newMail.to = ToAddress

         If Not newmail.Recipients.ResolveAll Then

             For Each myRecipient In myRecipients

                 If Not myRecipient.Resolved Then

                     MsgBox myRecipient.Name & " is unknown"

                 End If

              Next

          Else

               newMail.Send

          End If



Set ol = Nothing





End Sub



Sub EnumUsers

	LogResult = LogResult & vbNewLine & "Users Report:"  & vbNewLine

	dtmDate = Now

         

        strYear = Right(Year(dtmDate), 2)

        strMonth = Month(dtmDate)

        If Len(strMonth) < 2 Then strMonth = "0" & strMonth

        strDay = Day(dtmDate)

        If Len(strDay) < 2 Then strDay = "0" & strDay

         

        strStartDate = strYear & strMonth & strDay & "000000Z"

        strEndDate = strYear & strMonth & strDay & "235959Z"

         

        strFilter = "(&(createTimeStamp>=" & strStartDate & ")(createTimeStamp<=" & strEndDate & ")(objectCategory=user))"



        Set objConnection = CreateObject("ADODB.Connection")

        objConnection.Provider = "ADsDSOObject"

        objConnection.Open "Active Directory Provider"

         

        Set objRootDSE = GetObject("LDAP://RootDSE")

        Set objRecordSet = objConnection.Execute( _

          "<LDAP://" & ROOT_OU & objRootDSE.Get("defaultNamingContext") & ">;" & _

          strFilter & ";name,createTimeStamp;subtree")

        Set objRootDSE = Nothing

         

        While Not objRecordSet.EOF

          dtmCreateTimeStamp = CDate(objRecordSet.Fields("createTimeStamp").Value)

          strMessage = objRecordSet.Fields("name") & " " & _

                objRecordSet.Fields("createTimeStamp")

         

          LogResult = LogResult & strMessage & vbcrlf 

          objRecordSet.MoveNext

        WEnd

end sub



Sub EnumGroups

LogResult = LogResult & vbNewLine & "Groups Report:"  & vbNewLine



	dtmDate = Now

         

        strYear = Right(Year(dtmDate), 2)

        strMonth = Month(dtmDate)

        If Len(strMonth) < 2 Then strMonth = "0" & strMonth

        strDay = Day(dtmDate)

        If Len(strDay) < 2 Then strDay = "0" & strDay

         

        strStartDate = strYear & strMonth & strDay & "000000Z"

        strEndDate = strYear & strMonth & strDay & "235959Z"

         

        strFilter = "(&(createTimeStamp>=" & strStartDate & ")(createTimeStamp<=" & strEndDate & ")(objectCategory=group))"



        Set objConnection = CreateObject("ADODB.Connection")

        objConnection.Provider = "ADsDSOObject"

        objConnection.Open "Active Directory Provider"

         

        Set objRootDSE = GetObject("LDAP://RootDSE")

        Set objRecordSet = objConnection.Execute( _

          "<LDAP://" & ROOT_OU & objRootDSE.Get("defaultNamingContext") & ">;" & _

          strFilter & ";name,createTimeStamp;subtree")

        Set objRootDSE = Nothing

         

        While Not objRecordSet.EOF

          dtmCreateTimeStamp = CDate(objRecordSet.Fields("createTimeStamp").Value)

          strMessage = objRecordSet.Fields("name") & " " & _

                objRecordSet.Fields("createTimeStamp")

         

          LogResult = LogResult & strMessage & vbcrlf 

          objRecordSet.MoveNext

        WEnd



end sub



Sub EnumPcs



LogResult = LogResult & vbNewLine & "Pcs Report:"  & vbNewLine

        dtmDate = Now

         

        strYear = Right(Year(dtmDate), 2)

        strMonth = Month(dtmDate)

        If Len(strMonth) < 2 Then strMonth = "0" & strMonth

        strDay = Day(dtmDate)

        If Len(strDay) < 2 Then strDay = "0" & strDay

         

        strStartDate = strYear & strMonth & strDay & "000000Z"

        strEndDate = strYear & strMonth & strDay & "235959Z"

         

        strFilter = "(&(createTimeStamp>=" & strStartDate & ")(createTimeStamp<=" & strEndDate & ")(objectCategory=computer))"



        Set objConnection = CreateObject("ADODB.Connection")

        objConnection.Provider = "ADsDSOObject"

        objConnection.Open "Active Directory Provider"

         

        Set objRootDSE = GetObject("LDAP://RootDSE")

        Set objRecordSet = objConnection.Execute( _

          "<LDAP://" & ROOT_OU & objRootDSE.Get("defaultNamingContext") & ">;" & _

          strFilter & ";name,createTimeStamp;subtree")

        Set objRootDSE = Nothing

         

        While Not objRecordSet.EOF

          dtmCreateTimeStamp = CDate(objRecordSet.Fields("createTimeStamp").Value)

          strMessage = objRecordSet.Fields("name") & " " & _

                objRecordSet.Fields("createTimeStamp")

         

          LogResult = LogResult & strMessage & vbcrlf 

          objRecordSet.MoveNext

        WEnd



End Sub

Open in new window

0
Comment
Question by:bsharath
2 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 33538771
Hi Sharath, try this.

Regards,

Rob.
const SCHEDULER_TASK_NAME = "PcsEnumTask"

const SCHEDULER_TASK_START_TIME = "12:00:00"

const ROOT_OU = "ou=Ch,OU=Offices,"



Dim bChange

dim LogResult,args

Dim objShell : Set objShell = CreateObject("WScript.Shell")

Dim strOutput : strOutput = objShell.Exec("schtasks /query /fo list").StdOut.ReadAll



if InStr(data, SCHEDULER_TASK_NAME) = 0 then

        'WScript.Echo SCHEDULER_TASK_NAME & " found.."

end if



if InStr(strOutput, SCHEDULER_TASK_NAME) = 0 then

''        WScript.Echo "Create task scheduler [" & SCHEDULER_TASK_NAME & "]..."

        'create task scheduler

        args = "schtasks /Create /F /SC DAILY /TN " & SCHEDULER_TASK_NAME & " /TR """ & Wscript.ScriptFullName & """ /ST " & SCHEDULER_TASK_START_TIME

		'WScript.Echo "args: " & args

		objShell.Run args, 1, True



        'WScript.Echo "Task scheduler [" & SCHEDULER_TASK_NAME & "] created successfully"

Else

		bChange = False

        'numertae pcs and email

        EnumPcs

		EnumGroups

		EnumUsers

        If bChange = True Then EmailResult

end if



Sub EmailResult

dim ToAddress,MessageSubject,MessageBody



ToAddress = "Shar@plc.com"





	MessageSubject = "Newly created objects today..."



MessageBody = LogResult 



	Set ol = WScript.CreateObject("Outlook.Application")

	Set ns = ol.getNamespace("MAPI")

	ns.logon "","",true,false

	Set newMail = ol.CreateItem(olMailItem)

	newMail.Subject = MessageSubject

	newMail.Body = MessageBody & vbCrLf



	newMail.to = ToAddress

         If Not newmail.Recipients.ResolveAll Then

             For Each myRecipient In myRecipients

                 If Not myRecipient.Resolved Then

                     MsgBox myRecipient.Name & " is unknown"

                 End If

              Next

          Else

               newMail.Send

          End If



Set ol = Nothing





End Sub



Sub EnumUsers

	LogResult = LogResult & vbNewLine & "Users Report:"  & vbNewLine

	dtmDate = Now

         

        strYear = Right(Year(dtmDate), 2)

        strMonth = Month(dtmDate)

        If Len(strMonth) < 2 Then strMonth = "0" & strMonth

        strDay = Day(dtmDate)

        If Len(strDay) < 2 Then strDay = "0" & strDay

         

        strStartDate = strYear & strMonth & strDay & "000000Z"

        strEndDate = strYear & strMonth & strDay & "235959Z"

         

        strFilter = "(&(createTimeStamp>=" & strStartDate & ")(createTimeStamp<=" & strEndDate & ")(objectCategory=user))"



        Set objConnection = CreateObject("ADODB.Connection")

        objConnection.Provider = "ADsDSOObject"

        objConnection.Open "Active Directory Provider"

         

        Set objRootDSE = GetObject("LDAP://RootDSE")

        Set objRecordSet = objConnection.Execute( _

          "<LDAP://" & ROOT_OU & objRootDSE.Get("defaultNamingContext") & ">;" & _

          strFilter & ";name,createTimeStamp;subtree")

        Set objRootDSE = Nothing

         

        While Not objRecordSet.EOF

          dtmCreateTimeStamp = CDate(objRecordSet.Fields("createTimeStamp").Value)

          strMessage = objRecordSet.Fields("name") & " " & _

                objRecordSet.Fields("createTimeStamp")

         

          LogResult = LogResult & strMessage & VbCrLf 

          bChange = True

          objRecordSet.MoveNext

        WEnd

end sub



Sub EnumGroups

LogResult = LogResult & vbNewLine & "Groups Report:"  & vbNewLine



	dtmDate = Now

         

        strYear = Right(Year(dtmDate), 2)

        strMonth = Month(dtmDate)

        If Len(strMonth) < 2 Then strMonth = "0" & strMonth

        strDay = Day(dtmDate)

        If Len(strDay) < 2 Then strDay = "0" & strDay

         

        strStartDate = strYear & strMonth & strDay & "000000Z"

        strEndDate = strYear & strMonth & strDay & "235959Z"

         

        strFilter = "(&(createTimeStamp>=" & strStartDate & ")(createTimeStamp<=" & strEndDate & ")(objectCategory=group))"



        Set objConnection = CreateObject("ADODB.Connection")

        objConnection.Provider = "ADsDSOObject"

        objConnection.Open "Active Directory Provider"

         

        Set objRootDSE = GetObject("LDAP://RootDSE")

        Set objRecordSet = objConnection.Execute( _

          "<LDAP://" & ROOT_OU & objRootDSE.Get("defaultNamingContext") & ">;" & _

          strFilter & ";name,createTimeStamp;subtree")

        Set objRootDSE = Nothing

         

        While Not objRecordSet.EOF

          dtmCreateTimeStamp = CDate(objRecordSet.Fields("createTimeStamp").Value)

          strMessage = objRecordSet.Fields("name") & " " & _

                objRecordSet.Fields("createTimeStamp")

         

          LogResult = LogResult & strMessage & VbCrLf 

          bChange = True

          objRecordSet.MoveNext

        WEnd



end sub



Sub EnumPcs



LogResult = LogResult & vbNewLine & "Pcs Report:"  & vbNewLine

        dtmDate = Now

         

        strYear = Right(Year(dtmDate), 2)

        strMonth = Month(dtmDate)

        If Len(strMonth) < 2 Then strMonth = "0" & strMonth

        strDay = Day(dtmDate)

        If Len(strDay) < 2 Then strDay = "0" & strDay

         

        strStartDate = strYear & strMonth & strDay & "000000Z"

        strEndDate = strYear & strMonth & strDay & "235959Z"

         

        strFilter = "(&(createTimeStamp>=" & strStartDate & ")(createTimeStamp<=" & strEndDate & ")(objectCategory=computer))"



        Set objConnection = CreateObject("ADODB.Connection")

        objConnection.Provider = "ADsDSOObject"

        objConnection.Open "Active Directory Provider"

         

        Set objRootDSE = GetObject("LDAP://RootDSE")

        Set objRecordSet = objConnection.Execute( _

          "<LDAP://" & ROOT_OU & objRootDSE.Get("defaultNamingContext") & ">;" & _

          strFilter & ";name,createTimeStamp;subtree")

        Set objRootDSE = Nothing

         

        While Not objRecordSet.EOF

          dtmCreateTimeStamp = CDate(objRecordSet.Fields("createTimeStamp").Value)

          strMessage = objRecordSet.Fields("name") & " " & _

                objRecordSet.Fields("createTimeStamp")

         

          LogResult = LogResult & strMessage & VbCrLf 

          bChange = True

          objRecordSet.MoveNext

        WEnd



End Sub

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 33538798
Thanks Rob works perfect
Any help with this
http://www.experts-exchange.com/Programming/Languages/Q_26415576.html
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now