Solved

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

Posted on 2006-11-07
10
782 Views
Last Modified: 2012-06-21
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
Comment
Question by:andreni78
  • 4
  • 2
  • 2
10 Comments
 
LVL 6

Expert Comment

by:sodalitas
ID: 17891143
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
 
LVL 11

Expert Comment

by:ethoths
ID: 17891290
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
 

Author Comment

by:andreni78
ID: 17898725
thanks sodalitas, ethoths
I run the above code in a web application, not a window app. I still get nothing with your suggest.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 6

Expert Comment

by:sodalitas
ID: 17898838
Can you post the results of the debugging steps?
0
 
LVL 11

Expert Comment

by:ethoths
ID: 17898865
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
 
LVL 11

Expert Comment

by:ethoths
ID: 17898913
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
 

Author Comment

by:andreni78
ID: 17899198
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
 
LVL 11

Accepted Solution

by:
ethoths earned 500 total points
ID: 17899645
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

792 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