Pergunta

I am trying to use the Windows Process.start service in Visual Studios 2005 to call Windows task scheduler (schtasks) which calls the bat file. The process works fine except that the bat file takes in parameters but it won't work when I am trying to pass the parameters into the bat file.

 public string RunSchtasks(string MachineName) 
{
    ErrorMessage = null;
    Process myProcess = new Process();
    ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("schtasks");
    myProcessStartInfo.UseShellExecute = false;
    myProcessStartInfo.RedirectStandardError = true;
    myProcessStartInfo.RedirectStandardOutput = true;
    myProcessStartInfo.Arguments = "/run /s Machinename /tn mytest ParameterToPass2Bat"; 

   myProcess.StartInfo = myProcessStartInfo;

    try
    {
        myProcess.Start();
        StreamReader myStreamReader1 = myProcess.StandardOutput;
        string QueryResult = myProcess.StandardOutput.ReadToEnd();
    }

My code runs fine without the ParameterToPass2Bat part. If I want to pass in this parameter into the bat file, it wouldn't take it. Does anyone know how to do it so that the bat file takes in the parameter through Schtasks?

Thanks!!!

Foi útil?

Solução

schtasks.exe doesn't have any command-line capability for passing arguments into the scheduled task. One possible way to do it is to save the argument text in a (temporary) bat file of its own with contents specially crafted to set it as an environment variable. Then all you have to do is call the argument bat file from the scheduled task bat file and the environment variable will be set.

The contents of argument bat file would look like:

set parm=ParameterToPass2

The contents of your scheduled task bat file would then execute the argument bat file:

C:\wherever\argument.bat
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top