• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 790
  • Last Modified:

how can i invoke schtasks.exe command from a web application

My code is:

String FileName =Server.MapPath("batdir")+"\\firstcommand.bat";
            FileContent = "schtasks.exe /create /tn \"task1\" /tr \""+FileName+"\" /sc daily " ;
            FileInfo taskfile = new FileInfo(FileName);
            if (taskfile.Exists) taskfile.Delete();
            FileStream fs = new FileStream(FileName, FileMode.CreateNew, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(FileContent);
            sw.Close();
            fs.Close();
            System.Diagnostics.Process.Start(FileName);

but when this code runs, nothing happen. Does anybody help me?
0
andreni78
Asked:
andreni78
  • 4
  • 2
  • 2
1 Solution
 
sodalitasCommented:
Step through your code in debug mode.

After line 1 check that the value of FileName is what you expect.
After line 2 check that the value of FileContent is what you expect.
After line 3 check that the value of taskfile is the file object.
After line 4 check that the file is deleted.

<you should be cleaning up taskfile here>

After line 5 check that an empty file has been created.
After line 7 check the contents of your file.

wrap the entire block of code in a Try..Catch ex as Exception..Finally block and put a breakpoint in the Catch section and if you are getting in there, check the value of ex.
0
 
ethothsCommented:
Try this...

Dim myProcess As System.Diagnostics.Process = new System.Diagnostics.Process()
myProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
myProcess.Start

It might be starting th eprocess but in a hidden manner. You could also use the task manager to see if the process is being created but you'd need to know the process name.
0
 
andreni78Author Commented:
thanks sodalitas, ethoths
I run the above code in a web application, not a window app. I still get nothing with your suggest.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
sodalitasCommented:
Can you post the results of the debugging steps?
0
 
ethothsCommented:
An ASP.Net app hey, are you expecting this to run on the client or the server?

I'm assuming the server.

I'm not sure what schtasks.exe  is but it looks liek a console app to me so you need to open a console window if you want to see any output.
0
 
ethothsCommented:
Hang on - I thought it might have something to do with scheduled tasks. You need admin rights to run this app. A web app runs under MACHINENAME\ASPNET (or NETWORK_SERVICE in 2003).

You need to impersonate or use the some programmatic credentials.

0
 
andreni78Author Commented:
hi ethoths
I don't know how to have admin rights with programmatic credentials. Can you give me a code snippet. Thanks in advandce
0
 
ethothsCommented:
Give this a go.

        Dim p As New System.Diagnostics.Process
        p.StartInfo.UserName = "Administrator"
        Dim pas As New System.Security.SecureString
        Dim c As Char
        For Each c In "Password"
            pas.AppendChar(c)
        Next
        p.Start(FileName)
0

Featured Post

Technology Partners: 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!

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now