Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3724
  • Last Modified:

Run vbscript from C# Web App.

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
mdcadu
Asked:
mdcadu
  • 7
  • 6
2 Solutions
 
jjardineCommented:
Have you tried not using a securestring and just a string?
0
 
mdcaduAuthor Commented:
Yes.  I get a build error:  "Cannot implicitly convert type 'string' to 'System.Security.SecureString"
0
 
mdcaduAuthor Commented:
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
jjardineCommented:
Try executing it like this....


process.Start(@"C:\WINDOWS\System32\WScript.exe " + "\"" + @"D:\Projects\Pub\WebServices\MoHuntEdWebSvc\SCRIPTFILES\CreateFTPStudentFile.vbs" + "\"");
0
 
mdcaduAuthor Commented:
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
 
jjardineCommented:
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
 
mdcaduAuthor Commented:
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
 
jjardineCommented:
What if you change the file in your last example to WScript instead of cscript?  Also,  what happens if you UseShellExecute = true?
0
 
jjardineCommented:
Try not setting the working directory either.   maybe that is throwing off your full D path in the arguments
0
 
mdcaduAuthor Commented:
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
 
mdcaduAuthor Commented:
   
                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
 
jjardineCommented:
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
 
mdcaduAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now