mdcadu
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.
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();
Have you tried not using a securestring and just a string?
ASKER
Yes. I get a build error: "Cannot implicitly convert type 'string' to 'System.Security.SecureStr ing"
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.
"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.ex e " + "\"" + @"D:\Projects\Pub\WebServi ces\MoHunt EdWebSvc\S CRIPTFILES \CreateFTP StudentFil e.vbs" + "\"");
process.Start(@"C:\WINDOWS
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?
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.ex e " + @"D:\Projects\Pub\WebServi ces\MoHunt EdWebSvc\S CRIPTFILES \CreateFTP StudentFil e.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.ex e");
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.
process.Start(@"C:\WINDOWS
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
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.
ASKER
When I just use process.Start(@"C:\Windows \System32\ WScript.ex e"); error during compile "Static member 'System.Diagnostics.Proces s.String(s tring)' 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\W script.exe "); and the application compiled and ran without errors.
So I then tried: System.Diagnostics.Process .Start(@"C :\WINDOWS\ System32\W Script.exe " + @"D:\Projects\Pub\WebServi ces\MoHunt EdWebSvc\S CRIPTFILES \CreateFTP StudentFil e.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.WorkingD irectory = @"D:\Projects\Pub\WebServi ces\MoHunt EdWebSvc\S CRIPTFILES ";
process.StartInfo.FileName = @"cscript.exe";
process.StartInfo.UseShell Execute = false;
process.StartInfo.CreateNo Window = true;
process.StartInfo.Argument s = "D:\\Projects\\Pub\\WebSer vices\\MoH untEdWebSv c\\SCRIPTF ILES\\Crea teFTPStude ntFile.vbs ";
process.StartInfo.Domain = "mydomain";
process.StartInfo.UserName = "myuserid";
_password = "mypassword";
process.StartInfo.Password = new System.Security.SecureStri ng();
foreach (char c in _password)
{
process.StartInfo.Password .AppendCha r(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\WebService s\MoHuntEd WebSvc\SCR IPTFILES\C reateFTPSt udentFile. vbs
the file will run and produce the results as expected.
I really appreciate your time and help.
So I then tried: System.Diagnostics.Process
I have also tried this:
Process process = new Process();
process.StartInfo.WorkingD
process.StartInfo.FileName
process.StartInfo.UseShell
process.StartInfo.CreateNo
process.StartInfo.Argument
process.StartInfo.Domain = "mydomain";
process.StartInfo.UserName
_password = "mypassword";
process.StartInfo.Password
foreach (char c in _password)
{
process.StartInfo.Password
}
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\WebService
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
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.UseShell Execute = false;
process.StartInfo.CreateNo Window = true;
process.StartInfo.Argument s = "D:\\Projects\\Pub\\WebSer vices\\MoH untEdWebSv c\\SCRIPTF ILES\\Crea teFTPStude ntFile.vbs ";
process.StartInfo.Domain = "mydomain";
process.StartInfo.UserName = "myuserid";
_password = "mypassword";
process.StartInfo.Password = new System.Security.SecureStri ng();
foreach (char c in _password)
{
process.StartInfo.Password .AppendCha r(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?
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
process.StartInfo.UseShell
process.StartInfo.CreateNo
process.StartInfo.Argument
process.StartInfo.Domain = "mydomain";
process.StartInfo.UserName
_password = "mypassword";
process.StartInfo.Password
foreach (char c in _password)
{
process.StartInfo.Password
}
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?
ASKER
Process process = new Process();
process.StartInfo.FileName
process.StartInfo.UseShell
process.StartInfo.CreateNo
process.StartInfo.Argument
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.