Solved

Run vbscript from C# Web App.

Posted on 2009-04-06
13
3,286 Views
Last Modified: 2012-05-06
I have a .vbs file that uses the WScript.exe to map a drive to the network with the correct userid and password.  Then will login to sqlplus  and execute an Oracle command which outputs a text file to the mapped drive.  If I double click the .vbs file, everything works fine.  Now I need to have a webmethod do the same behavior as double clicking the .vbs file.  

I'm using the System.Diagnosics namespace to use the Process class.  I will get an error of "Logon failure: unknown user name or bad password".

Is it because the password gets encrypted?  I am using VS2005, in IIS annonymous access is allowed on the virtual directory.

I have been making changes at this for 5 days and would appreciate any understanding.  I move from one error to another.
Process process = new Process();
process.StartInfo.WorkingDirectory =   @"D:\Projects\Pub\WebServices\MoHuntEdWebSvc\SCRIPTFILES";
 process.StartInfo.FileName = @"D:\Projects\Pub\WebServices\MoHuntEdWebSvc\SCRIPTFILES\CreateFTPStudentFile.vbs";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.CreateNoWindow = false;
                 process.StartInfo.Domain = "mydomainname";
                process.StartInfo.UserName = "myuserid";
                _password = "mypassword";
                process.StartInfo.Password = new System.Security.SecureString();
              foreach (char c in _password)
               {
                   process.StartInfo.Password.AppendChar(c);
               }
                
               process.Start();

Open in new window

0
Comment
Question by:mdcadu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 14

Expert Comment

by:jjardine
ID: 24081288
Have you tried not using a securestring and just a string?
0
 

Author Comment

by:mdcadu
ID: 24081346
Yes.  I get a build error:  "Cannot implicitly convert type 'string' to 'System.Security.SecureString"
0
 

Author Comment

by:mdcadu
ID: 24081554
If I comment out the code for the username, domain and password  I get the error:
"The specified executable is not a valid Win32 application.".  I realize that trying to run a .vbs is not an .exe.  I can tell the filename to be "sqlplus.exe" and I will see in the task Manager that SQLPlus.exe is running with the ASPNet account; however, all the code in my .vbs would have to be recreated.    I am running this in Studio 2005 on Windows XP Professional SP2.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 14

Expert Comment

by:jjardine
ID: 24082587
Try executing it like this....


process.Start(@"C:\WINDOWS\System32\WScript.exe " + "\"" + @"D:\Projects\Pub\WebServices\MoHuntEdWebSvc\SCRIPTFILES\CreateFTPStudentFile.vbs" + "\"");
0
 

Author Comment

by:mdcadu
ID: 24088554
New Day...  I'm trying again....
Did as you last suggested.  Compiled fine.  Now get an error: "The system cannot find the file specified".

I checked at the WScript.exe is at the location as well as the ...vbs file.    I have ran this both in Studio and from a browser using localhost.

Any more suggestions?

0
 
LVL 14

Expert Comment

by:jjardine
ID: 24089699
try this...

process.Start(@"C:\WINDOWS\System32\WScript.exe " + @"D:\Projects\Pub\WebServices\MoHuntEdWebSvc\SCRIPTFILES\CreateFTPStudentFile.vbs" );

Also, lets try to determine which file it can't find.   can you run it without the vbs file as a parameter  so just this:

process.Start(@"C:\Windows\System32\WScript.exe");  

See if that blows up.  If it doesn't then maybe there is a problem with the other file.  I modified the first string above to remove some quotes that aren't needed since you don't have any spaces in your text.
0
 

Author Comment

by:mdcadu
ID: 24089934
When I just use process.Start(@"C:\Windows\System32\WScript.exe");    error during compile "Static member 'System.Diagnostics.Process.String(string)' cannot be accessed with an instance reference; quailify it with a type name instead.  So I then tried System.Diagnostics.Process.Start(@"C:\WINDOWS\System32\Wscript.exe");  and the application compiled and ran without errors.

So I then tried: System.Diagnostics.Process.Start(@"C:\WINDOWS\System32\WScript.exe " + @"D:\Projects\Pub\WebServices\MoHuntEdWebSvc\SCRIPTFILES\CreateFTPStudentFile.vbs" );  and I get the error of "The system cannot find the file specified."

I have also tried this:  
Process process = new Process();
               process.StartInfo.WorkingDirectory = @"D:\Projects\Pub\WebServices\MoHuntEdWebSvc\SCRIPTFILES";
                process.StartInfo.FileName = @"cscript.exe";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.Arguments = "D:\\Projects\\Pub\\WebServices\\MoHuntEdWebSvc\\SCRIPTFILES\\CreateFTPStudentFile.vbs";
                process.StartInfo.Domain = "mydomain";
                process.StartInfo.UserName = "myuserid";
                _password = "mypassword";
                process.StartInfo.Password = new System.Security.SecureString();
                foreach (char c in _password)
                {
                    process.StartInfo.Password.AppendChar(c);
                  }
               
                    process.Start();
 
RESULTS:  a window comes up with error:  cscript.exe - application Error
 The application failed to initialize properly (0xc0000142). Click on OK to terminate the application

If I go to the Run command prompt and type in:
cscript.exe D:\Projects\Pub\WebServices\MoHuntEdWebSvc\SCRIPTFILES\CreateFTPStudentFile.vbs
the file will run and produce the results as expected.


I really appreciate your time and help.
0
 
LVL 14

Expert Comment

by:jjardine
ID: 24090226
What if you change the file in your last example to WScript instead of cscript?  Also,  what happens if you UseShellExecute = true?
0
 
LVL 14

Expert Comment

by:jjardine
ID: 24090231
Try not setting the working directory either.   maybe that is throwing off your full D path in the arguments
0
 

Author Comment

by:mdcadu
ID: 24090643
If I set UseShellExecute = true, compiles, but throws error in the try/catch on the process.start.
The Process object must have the UseShellExecute property set to false in order to start a process as a user

so I took off the working directory setting.   My code now looks like this:
Process process = new Process();
                process.StartInfo.FileName = @"Wscript.exe";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.Arguments = "D:\\Projects\\Pub\\WebServices\\MoHuntEdWebSvc\\SCRIPTFILES\\CreateFTPStudentFile.vbs";
                process.StartInfo.Domain = "mydomain";
                process.StartInfo.UserName = "myuserid";
                _password = "mypassword";
                process.StartInfo.Password = new System.Security.SecureString();
                foreach (char c in _password)
                {
                    process.StartInfo.Password.AppendChar(c);
                }

                process.Start();

RESULTS:  a window comes up with error:  cscript.exe - application Error
 The application failed to initialize properly (0xc0000142). Click on OK to terminate the application


This is suppose to work with a web app and not just a windows app, right?
0
 

Author Comment

by:mdcadu
ID: 24090689
   
                Process process = new Process();
                process.StartInfo.FileName = @"Wscript.exe";
                process.StartInfo.UseShellExecute = true;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.Arguments = "D:\\Projects\\Pub\\WebServices\\MoHuntEdWebSvc\\SCRIPTFILES\\CreateFTPStudentFile.vbs";
                process.Start();

creates no errors; however, it doesn't appear that my .vbs file ran.  I'm looking for my text file and it didn't get written.
0
 
LVL 14

Assisted Solution

by:jjardine
jjardine earned 400 total points
ID: 24090801
So it appears to run if you don't specify a user name and password, but throws an error if you do specify the username and password? hmm..   maybe you can use impersonation in the web app to run as a a user that has the rights to execute the vbs script or write to that directory.   Maybe this will help... http://support.microsoft.com/kb/306158
0
 

Accepted Solution

by:
mdcadu earned 0 total points
ID: 24091356
I looked at the link you posted and have coded for the "Impersonate a Specific User in Code".  I can see where the impersonation is set up with my network login on the domain.  When i run the code, still nothing.  I looked in the Task Manager while executing the process.start() and it is running with the ASPNET user for the WScript.exe and the sqlplus.exe (located in the .vbs file).  So it appears that it is running the vbs file (otherwise, how would it know to run sql plus(sqlpllus.exe).  The drive isn't being mapped (inside .vbs) and I would say that aspnet user doesn't have a valid login to sql plus.

Here is the latest code (with the impersonation).
if(impersonateValidUser("myuserid", "mydomain", "mypassword"))
{
      //Insert your code that runs under the security context of a specific user here.
        try
        {
                  Process process = new Process();
                  process.StartInfo.FileName = @"Wscript.exe";
                  process.StartInfo.UseShellExecute = true;
                  process.StartInfo.CreateNoWindow = true;
                  process.StartInfo.Arguments  = "D:\\Projects\\Pub\\WebServices\\MoHuntEdWebSvc\\SCRIPTFILES\\CreateFTPStudentFile.vbs";
                 process.Start();
                 process.WaitForExit();
                 loadstatus = "OK";
        }
        catch (Exception e)
        {
            loadstatus = "Failure in preparing FTP Student File: " + e.Message;
        }
              undoImpersonation();
 }
 else
 {
       //Your impersonation failed. Therefore, include a fail-safe mechanism here.
 }

More ideas.
0

Featured Post

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!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 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