Solved

Looking for a way to run a batch after an email arrives????

Posted on 2011-09-19
21
1,110 Views
Last Modified: 2013-12-18
Anyone have any way to run a batch file (.bat) or generate a file in a watched folder after an email arrives in your inbox?

I'm looking to kick off a process after an email arrives from one of our server admins.

I'm open to suggestions or other ideas but it would need to based upon receiving an email from the admin.

Thanks.

NG,

0
Comment
Question by:nike_golf
  • 8
  • 5
  • 5
  • +1
21 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 36565134
Don't completely understand...

A mail comes in and then you want to start a (seemingly unrelated) batch file? Doing what exactly? Does it have to read the mail? Does it have to respond to the mail? Or is just a trigger enough? Why does it have to be a batch file?

Heavily assuming here: you could write an agent that opens the mail, reads it and generates a batch file in some external directory on disk, where a scheduled task picks it up and processes it.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36565586
Yes, or have the mail sent to a special mailbox which is a mail-in database instead and then have an agent triggered by the mail coming in then (which means it does not have to run for each mail in your normal inbox).
0
 
LVL 10

Expert Comment

by:doninja
ID: 36566049
If running on server you can create an agent that is scheduled to run on event (after new mail arrives)
In this agent run the test against from field to see if it comes from admin then use the shell() command to run your batch script.

Make sure agent run time security is set to level 2. allow restricted operations.

On a server the agent will run as the localsystem account unless server service is setup to run as a specific account.
0
 
LVL 13

Author Comment

by:nike_golf
ID: 36566062
Sorry for being so vague.

A mail comes in and then you want to start a (seemingly unrelated) batch file? yes.

Doing what exactly? running some sql scripts

Does it have to read the mail? no, just to know who the sender was and what time it arrived, after 8:15 a.m.

Does it have to respond to the mail? no.

I have been looking at Agents but can't seem to get it to work maybe a little help and it could be the correct way to go. Also, I'm a bit lost on the correct syntax, etc.

The Agent seems to identify that an email arrives but never runs? If I "Test" it seems to identify that it has found a message that meets the criteria, so it appears to be setup correctly. I've attached pictures below.





image1.png
image2.png
image3.png
image4.png
image5.png
0
 
LVL 13

Author Comment

by:nike_golf
ID: 36566088
Meant to also attach the ACL.
image6.png
0
 
LVL 13

Author Comment

by:nike_golf
ID: 36567757
Since I'm very new to writing an Agent is there a quick way to get it to generate a text box with "hello world"?

NG,
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 36567935
It might seem funny, but it isn't: no, you can't do that, for the simple reason that the agent runs on the server.

Maybe this page can help you:
http://stackoverflow.com/questions/2291073/lotus-notes-export-emails-to-plain-text-file
0
 
LVL 10

Expert Comment

by:doninja
ID: 36568418
As Sjef mentions you would need administrative level access to make an agent run a script on the server on new mail as it would need to run with unrestricted access (adjust setting in image5)

it would be feasible to have a script run on a local workstation if you change the local setting to allow local scheduled agents (Preferences) and have the agent run when documents are created or modified. On agent schedule set on server to be local.

As for writing agents the basic help in Notes Designer is a good starting point but you will need some base programming knowledge to get you started writing in lotusscript or Java.

As a starting point you would need to look into NotesDocumentCollection and UnprocessedDocuments method to get a list of documents and then loop to look for the specific email you want.
0
 
LVL 13

Author Comment

by:nike_golf
ID: 36569466
Well I just got the Agent to run a "Simple Action". So, at least I know its working...

Would the sub below go in the "Initialize" section as opposed to the "Action" in the Agent? When is the "Initialize" section supposed to run?

Sub runbatch()
On Error Resume Next
Dim intTemp1 As Integer
intTemp1 = Shell("\\servername\Batch_Files\test.bat")
On Error Goto Errormessage
Exit Sub

Errormessage:
Msgbox "Batch file error !!"
Exit Sub
exit
End sub
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 36570521
I'd never run a batch file started by the server. The batch job may take a long time, so server operation could be severely hindered by it. Other agents might be blocked, the batch might be cancelled while executing, and worst case you might crash the server (unlikely). I'd always have some external batch processor scheduled to execute the batch files.

The script you supplied contains some mistakes and typos. Here a corrected one if you want to try it:

Sub Initialize
      On Error Goto Errormessage
      Dim intTemp1 As Integer
      intTemp1 = Shell("\\servername\Batch_Files\test.bat")
      Exit Sub

Errormessage:
      Msgbox "Batch file error !!" ' will be printed in the server's log.nsf database
      Exit Sub
End Sub
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 250 total points
ID: 36570622
Ruuning the shell command by the server will run it as the account the server runs as.  If this is run as local system then it will have no access to a share on another server such as this unless it is a null session share like "NETLOGON" directory is.  If the service runs as domain user account then you could apply suitable permissions to it for the other share.

Perhaps better would be to have the email trigger the creation of a flag file, and a scheduled task to run perioidcally on the server watching for that flag file and executing your batch file contents, i.e.

Dim filename As String
Dim filenum As Integer
filenum=Freefile
filename = "d:\batches\flagfile.txt"

If Dir$(filename)<>"" Then
      Msgbox "The flag file still exists and is being replaced"
End If

Open filename For Output As #filenum
      Print #filenum,"Flag file created " & Now
      Msgbox "Flag file created"
Close #filenum
      

And then in your batch you could do something like this... can obviously get lots more error checking in there and the like but depends what is in your existing batch

@echo off
set flagfile="d:\batches\flagfile.txt"

if exist %flagfile% (
  echo Flag file exists.  Do what you need to do here.
  pushd \\servername\batch_files
    call test.cmd
    if errorlevel 1 echo There was an error running job (%errorlevel%)
    del %flagfile%
  popd
) ELSE (
  echo No flag file exists
)


Or instead of calling the batch file from your other server just incoporate it in the batch here.

the "MSDOS" and "Windows Batch" areas here are very good for such scripting :-)
0
 
LVL 13

Author Comment

by:nike_golf
ID: 36570900
Thanks for ideas and hanging with me.

The strange thing I encountered was that I was only able to run a "Simple Action" script only once, I couldn't get it to run again. Maybe trying to run the batch file has something to do with that?

I think I follow that running the batch file on the server isn't a good thing which I now understand is where the batch file would be executed, not on the client machine. Is that because the agent runs on the server and not on the client?

So, assuming that is the case then the better route is to create a file in a watched folder which is what dragon-it is suggesting.

"Perhaps better would be to have the email trigger the creation of a flag file, and a scheduled task to run periodically on the server watching for that flag file and executing your batch file contents, i.e."


Would this code be placed in the "Initialize" area of the Agent?

Dim filename As String
Dim filenum As Integer
filenum=Freefile
filename = "d:\batches\flagfile.txt"

If Dir$(filename)<>"" Then
      Msgbox "The flag file still exists and is being replaced"
End If


Open filename For Output As #filenum
      Print #filenum,"Flag file created " & Now
      Msgbox "Flag file created"
Close #filenum
0
 
LVL 13

Author Comment

by:nike_golf
ID: 36570978
OK, making a little progress...

I created a new Agent and the inserted the below into the "Initialize" portion of the code. I created in the Document Section a condition to identify mail from me, sent the Agent properties to run on "After new mail has arrived" saved and then sent myself an email.

Dim filename As String
Dim filenum As Integer
filenum=Freefile
filename = "d:\batches\flagfile.txt"

If Dir$(filename)<>"" Then
      Msgbox "The flag file still exists and is being replaced"
End If

Open filename For Output As #filenum
      Print #filenum,"Flag file created " & Now
      Msgbox "Flag file created"
Close #filenum


Nothing... I got no file no messages, etc. So I took a look at the Agent Log for the Agent that I created and got the 1st image below "never ran".

After that I right clicked on the agent and clicked "Run". It created the file to my amazement... and I also looked at the log got the second screenshot.

So, it seems the "trigger" isn't firing???

image1.png
Untitled.png
0
 
LVL 13

Author Comment

by:nike_golf
ID: 36571034
OK, so something is up with "Add Condition"

I choose "Author" and then select my name from the address book. It does seem to work as the "trigger"...???


If I right click on the Agent and check the log this is in the log:

Running on new mail messages: 1 total
Found 0 document(s) that match search criteria
add-condition.png
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36571642
sorry, have been in bed.... Will look back when i can in a bit.
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 36572086
If you want to run a local agent: File/Preferences, select Basic Notes Client Configuration, Enable scheduled local agents

But: this only works with a local database, i.e. a replica of your mail database on the PC's hard disk. Plus, it won't work for mails being received, for the actual reception happens on the server. Hence, you might have to run a local agent triggered on When documents are created or modified. The agent then has to check whether it's a mail that falls in the category, and only then it'll run the batch. In that case, the flag file would serve little point, I think.

@Steve, what do you think?
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 36572090
@Steve: wake up man!!  :-)
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36572328
@Sjef: Been dealing with unpleasant things that come out of poorly 2 year olds while wife drops others at school...

Can't say I've ever used scheduled local agents as frankly I doubt they would be reliable -- aside from anything else lets say you are on holiday, is this presumably important enough script then not going to get run?!

As an aside what is this email and why does it need to be followed.  Is it some sort of log from a certain person or an instruction saying "run the job now" from a human.

In both cases could it a) the original job or log set off this one, or b) have the person sending the email instead press a button in a database or email say, or shortcut on their desktop?

Steve

0
 
LVL 13

Author Comment

by:nike_golf
ID: 36573092
Sorry I mistyped.

"I choose "Author" and then select my name from the address book. It does seem to work as the "trigger"...???"

This should have said "It doesn't seem to work as the trigger..."


"If you want to run a local agent: File/Preferences, select Basic Notes Client Configuration, Enable scheduled local agents"

This was not checked so I went ahead and checked it with similar results, the trigger didn't work.


I sent myself an email and then ran a manual test on the Agent, this is the message. Also, the "Condition" I used was "By Field" and then I found "From":

The following will occur when this agent is run:

'CN=my name/OU=Corporate/O=my company' is allowed to run Personal/Formula/Simple Agents on 'CN=MAIL01/O=my company'.
'CN=my name/OU=Corporate/O=my company' has restricted rights to run LotusScript/Java agents on 'CN=MAIL01/O=my company'.
Allow restricted operations flag is selected in this agent, but will be ignored because the signer does not have unrestricted rights.
The agent will run on behalf 'CN=my name/OU=Corporate/O=my company'
Agent will be run by the Agent Manager after new mail messages are delivered.
Home mail server for 'CN=my name/OU=Corporate/O=my company' is 'CN=MAIL01/O=my company'.

Started running agent 'DOR_Arrived' on 09/21/2011 07:00:37 AM
Running on new mail messages: 1 total
Found 1 document(s) that match search criteria
Testing:  Ran LotusScript code
Done running agent 'DOR_Arrived' on 09/21/2011 07:00:37 AM

From what I read the message was identified correctly as "new" and the "criteria" was met but the Agent itself didn't run it only when ran manually??


When I manually "Run" the Agent this is the output:

Started running agent 'DOR_Arrived' on 09/21/2011 07:06:21 AM
Running on new mail messages: 1 total
Found 1 document(s) that match search criteria
Ran LotusScript code
Done running agent 'DOR_Arrived' on 09/21/2011 07:06:21 AM

The file is generated, I can run this manually as many times as I want, each time the file is generated.


When I change the "Condition" to "By Author" and then choose my name I get the following:

The following will occur when this agent is run:

'CN=my name/OU=Corporate/O=my company' is allowed to run Personal/Formula/Simple Agents on 'CN=MAIL01/O=my company'.
'CN=my name/OU=Corporate/O=my company' has restricted rights to run LotusScript/Java agents on 'CN=MAIL01/O=my company'.
Allow restricted operations flag is selected in this agent, but will be ignored because the signer does not have unrestricted rights.
The agent will run on behalf 'CN=my name/OU=Corporate/O=my company'
Agent will be run by the Agent Manager after new mail messages are delivered.
Home mail server for 'CN=my name/OU=Corporate/O=my company' is 'CN=MAIL01/O=my company'.

Started running agent 'DOR_Arrived' on 09/21/2011 07:00:37 AM
Running on new mail messages: 1 total
Found 0 document(s) that match search criteria
Testing:  Ran LotusScript code
Done running agent 'DOR_Arrived' on 09/21/2011 07:00:37 AM

I assume this means it doesn't know how to identify the Author correctly... stupid.

Just trying to help you help me... I hope this is helping.

As an aside what is this email and why does it need to be followed.  Is it some sort of log from a certain person or an instruction saying "run the job now" from a human.

This is a system generated email sent to a list of peeps when a server job completes telling them the system is ready for use.
0
 
LVL 10

Accepted Solution

by:
doninja earned 250 total points
ID: 36574350
Looking at what you have posted this line

"CN=my name/OU=Corporate/O=my company' has restricted rights to run LotusScript/Java agents on 'CN=MAIL01/O=my company'"

says you do not have sufficient rights on the server (Unrestricted rights required). It worked locally as you do have unrestricted access to your own machine.

You will either have to be nice to server admins and get them to elevate your rights on the server, or get an admin to sign your agent once it is complete.

Or run as a scheduled only agent on local workstation when client is open and logged on as you.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36574507
too much to read on mobile but sounds like you are checking author field and might need to be different field.  why not select based on subject or something for now?  you could also create a view that only displays your relevant emails by filtering name/subject etc. except ones marked with a flag then process any documents in that view and mark the flag once processed so they only go once.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Lotus Notes 41 163
HP DP 7.03 and lotus collaboration suite 9 3 144
Synchronize IBM notes Internet Password with AD 9 342
lotus notes, exchange 7 105
Problem "Can you help me recover my changes?  I double-clicked the attachment, made changes, and then hit Save before closing it.  But when I try to re-open it, my changes are missing!"    Solution This solution opens the Outlook Secure Temp Fold…
This article covers general Notes 8.5 troubleshooting information including recreating the Notes\Data folder.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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

20 Experts available now in Live!

Get 1:1 Help Now