Link to home
Start Free TrialLog in
Avatar of mdcadu
mdcaduFlag for United States of America

asked on

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

Avatar of jjardine
jjardine
Flag of United States of America image

Have you tried not using a securestring and just a string?
Avatar of mdcadu

ASKER

Yes.  I get a build error:  "Cannot implicitly convert type 'string' to 'System.Security.SecureString"
Avatar of mdcadu

ASKER

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.
Try executing it like this....


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

ASKER

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?

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.
Avatar of mdcadu

ASKER

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.
What if you change the file in your last example to WScript instead of cscript?  Also,  what happens if you UseShellExecute = true?
Try not setting the working directory either.   maybe that is throwing off your full D path in the arguments
Avatar of mdcadu

ASKER

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?
Avatar of mdcadu

ASKER

   
                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.
SOLUTION
Avatar of jjardine
jjardine
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial