Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help breaking out of loop

Status
Not open for further replies.

deadlinez

Technical User
Mar 30, 2009
1
US
hey I need help I'm trying to get this script to listen for a specific user logging onto a large list of machines pulled from a text file and then email me. How should I break out of the Do While loop to actually send the email?

thanx
Code:
On Error Resume Next
strEmail = InPutBox ("Enter email address to send alerts to. Seperate multiple address' with a semicolon ;")
'strComputer = InPutBox  ("Enter Server Name:")
Set Fso = CreateObject("Scripting.FileSystemObject")
strFile = InPutBox ("Please enter the name of the text file that has the list of the PC you want to monitor:")
Set InputFile = fso.OpenTextFile(strFile)

Do While Not (InputFile.atEndOfStream)
strComputer = InputFile.ReadLine
'strEvent = InPutBox ("Enter Username to monitor:") 


Set objWMIService = GetObject("winmgmts:{(Security)}\\" & _
        strComputer & "\root\cimv2")

Set colMonitoredEvents = objWMIService.ExecNotificationQuery _    
    ("Select * from __InstanceCreationEvent Where " _
        & "TargetInstance ISA 'Win32_NTLogEvent' " _
            & "and TargetInstance.user = 'domain\\user.name' "_
		& "and TargetInstance.EventCode = '528' ")


Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
    SendMessage objLatestEvent.TargetInstance.Message
Loop


Sub SendMessage (sText) 
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "somebody@somewhere.com"
objEmail.To = strEmail
objEmail.Subject = "user.name has logged on to " & strComputer 
objEmail.Textbody =  objLatestEvent.TargetInstance.User & vbCrLf & objLatestEvent.TargetInstance.Message
'objEmail.AddAttachment ""
objEmail.Configuration.Fields.Item _
    ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 2
objEmail.Configuration.Fields.Item _
    ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL] = _
        "echosmtp1.echostar.com" 
objEmail.Configuration.Fields.Item _
    ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
objEmail.Configuration.Fields.Update
objEmail.Send
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top