Solved

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

Posted on 2006-11-07
10
781 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 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