?
Solved

VB.NET Shell Command calling .cmd file with Parameter and Passing to VBS file fails

Posted on 2010-01-04
14
Medium Priority
?
648 Views
Last Modified: 2012-05-08
I have a VB.NET application which outputs a file.  I have a .CMD file which it shell executes passing the name of the file it created.  This .CMD file in turn calls a VBS file passing it the paramter.  That VBS file in turn creates a CDO object and sends the file as an attachment.

The cmd to vbs with a parameter works fine from the command line but fails in the shell execute within the VB.net application.

Any advise on this would be greatly appreciated.

Thanks.
0
Comment
Question by:Howard Bash
  • 6
  • 4
  • 2
  • +1
14 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 26172704
"... but fails in the shell execute within the VB.net application."

Please show us your code that launches the whole process.
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 664 total points
ID: 26172865
I prefer using the System.Diagnostics.Process class to start an external process, like this:


Imports System
Imports System.Diagnostics

Public Class NetSend

  ''' -----------------------------------------------------------------------------
  ''' <summary>
  ''' Send a message to a user or a domain
  ''' </summary>
  ''' <remarks>
  '''	  The syntax of this command is:
  '''     NET SEND
  ''' {name | * | /DOMAIN[:name] | /USERS} message
  ''' </remarks>
  ''' <history>
  ''' 	[Bob]	6/7/2006	Created
  ''' </history>
  ''' -----------------------------------------------------------------------------
  Public Shared Sub SendMessage(ByVal name As String, ByVal domain As String, ByVal message As String)

    Dim process As New Process()

    Dim arguments As String = "SEND"

    If domain.Length > 0 Then
      arguments &= " /DOMAIN:" & domain
    End If

    process.StartInfo.FileName = "net.exe"
    process.StartInfo.Arguments = arguments

    process.StartInfo.CreateNoWindow = True
    process.StartInfo.ErrorDialog = False
    process.StartInfo.RedirectStandardOutput = False
    process.StartInfo.RedirectStandardInput = False
    process.StartInfo.RedirectStandardError = False
    process.StartInfo.UseShellExecute = False

    process.Start()

    process.WaitForExit(2000)

  End Sub

End Class

Open in new window

0
 
LVL 1

Author Comment

by:Howard Bash
ID: 26173021
This works from the command line (obtained from immediate window in debug mode "sPath & "EmailRetentionJobEnd.cmd " + sOutputFile"):

C:\mycode\CheckJournalFacetime\CheckJournalFacetime\bin\Release\EmailRetentionJobEnd.cmd C:\TEMP\EmailRetentionExceptionsReport_20100104_V10.xlsx

This does not:
            Shell(sPath & "EmailRetentionJobEnd.cmd " + sOutputFile, AppWinStyle.NormalFocus)


EmailRetentionJobEnd.cmd:
cscript //nologo EmailRetentionJobEnd.vbs %1


EmailRetentionJobEnd.vbs:
'------------------------------------------------------------------------
'EmailRetentionJobEnd.vbs
'------------------------------------------------------------------------

on Error Resume Next

= "hbash@somedomain.com" 'who are you mailing to
strMailto = "hbash@us.nomura.com" 'who are you strMailto = "hbash@somedomain.com" 'who are you mailing to
strMailFrom = "hbash@somedomain.com" 'reply address
strSubject = "OCS Email Retention Job" 'mail subject
strMsgBody = "Some Message Body Text Here!"
strSMTPServer = "mail.somedomain.com" 'set SMTP email server address here

Const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing", _
cdoSendUsingPort = 2, _
cdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver"

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const ForReading = 1
Const ForWriting = 2

'------------------------------------------------------------------------
'Get File with path of report file passed as commandline parameter
'------------------------------------------------------------------------
Dim ArgObj, sReportFile
Set ArgObj = WScript.Arguments

sReportFile = ArgObj(0)

'MsgBox "Attached file to send is " + sReportFile, 1, "VBS"

'------------------------------------------------------------------------
Call EmailFile

'------------------------------------------------------------------------


'------------------------------------------------------------------------
'Function EmailFile - email the warning file
'------------------------------------------------------------------------
Function EmailFile
Dim iMsg, iConf, Flds

'------------------------------------------------------------------------
'// Create the CDO connections.
'------------------------------------------------------------------------
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields

'------------------------------------------------------------------------
'// SMTP server configuration.
'------------------------------------------------------------------------
With Flds
.Item(cdoSendUsingMethod) = cdoSendUsingPort

'// Set the SMTP server address here.
.Item(cdoSMTPServer) = strSMTPServer
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Update
End With

'------------------------------------------------------------------------
'// Set the message properties.
'------------------------------------------------------------------------
With iMsg
Set .Configuration = iConf
.To = strMailTo
.From = strMailFrom
.Subject = strSubject
.TextBody = "Email Retention Status Report Completed  - " + CStr(Now())

.AddAttachment sReportFile

End With

'------------------------------------------------------------------------
' Send the message.
'------------------------------------------------------------------------
iMsg.Send ' send the message.

'MsgBox "Message Sent", 1, "VBS"

'If Err.Number <> 0 Then
'    Msgbox "Description: " &  Err.Description, 1, "VBS"
'    Err.Clear
'End If

'------------------------------------------------------------------------
'Clean up objects
'------------------------------------------------------------------------
Set iMsg = Nothing
Set iConf = Nothing
Set Flds = Nothing
'------------------------------------------------------------------------

End Function





0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 96

Expert Comment

by:Bob Learned
ID: 26173053
You will need to determine what errors that you are getting from executing the command file, which are not normally available.
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 1336 total points
ID: 26173161
if all you want to do is to send a e-mail, why not use built-in features: http://www.emoreau.com/Entries/Articles/2007/09/Using-SystemNetMail.aspx
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 26173738
As suggested by Bob already, try the Process class:

        Dim P As New Process
        P.StartInfo.FileName = sPath & "EmailRetentionJobEnd.cmd"
        P.StartInfo.WorkingDirectory = sPath
        P.StartInfo.Arguments = sOutputFile
        P.Start()
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 26173787
Aah, a fine group of experts that we have assembled on this one question!!  I would vote with Eric, and use the System.Net.Mail classes to do mail operations...
0
 
LVL 1

Author Comment

by:Howard Bash
ID: 26174609
I tried Eric's code and it worked great on my local pc.  However,  when I deployed it on an application server,  all the application ran and the calls to the code were made,  but no email arrived.  

Not sure why, but it appears that whatever the error is,  it is getting eaten.  Any advice on this would be greatly appreciated.

Thanks.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 26174647
Eaten exceptions depends on your code, and whether you have a Try/Catch block, without anything in the Catch portion...
0
 
LVL 1

Author Comment

by:Howard Bash
ID: 26174729
I have try catch blocks on all the routines.  Also, please note that the code works fine on my local pc and fails on the deployment machine.  

All the called routines catch and throw new exception up to the main routine which unravels the exceptions and displays a messagebox (a log would be better...).
0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 1336 total points
ID: 26174875
remove all exception handlers and you will see it!

Are you sure that the credentials (for the SMTP server) on your application server are the same as on your PC? Have you tried to send a e-mail to the same domain as the application server?
0
 
LVL 1

Author Comment

by:Howard Bash
ID: 26175293
The credentials are different.  I login to the application server as an admin.  However,  the CMD --> VBS with CDO does run and I do receive the emails.

I will look into removing the exception handlers, although I would have expected to see the messagebox if any errors were raised.

0
 
LVL 1

Author Comment

by:Howard Bash
ID: 26180573
Ok.  It is working with Eric's code.  I boneheaded on the recipient list and so it was working but I was not getting the email (DUH!).  So thanks folks.
0
 
LVL 1

Author Closing Comment

by:Howard Bash
ID: 31672466
All good advice.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

862 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