Solved

Run vbscript from C# Web App.

Posted on 2009-04-06
13
3,152 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
  • 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 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

21 Experts available now in Live!

Get 1:1 Help Now