I am using the following code to change the 'Run As:' username and password for a scheduled task on a remote host.
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "SCHTASKS.exe";
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
//p.StartInfo.Arguments = String.Format("/Change /TN {0} /RU {1} /RP {2}",ScheduledTaskName,userName,password);
p.StartInfo.Arguments = String.Format(
"/Change /S {0} /TN {1} /TR {2} /RU {3}\\{4} /RP {5}",
MachineName, ScheduledTaskName, taskPath, activeDirectoryDomainName, userName, password);
p.Start();
// Read the error stream first and then wait.
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
I have a couple of questions:
Right-click the Task Scheduler service, and then click Properties. On the General tab, make sure that the startup type is set to automatic, and that the service status is Started. If the service is not running, click Start.
To retrieve the existing tasks in the task scheduler using PowerShell, we can use the PowerShell command Get-ScheduledTask. We can use the Task Scheduler GUI to retrieve the scheduled tasks. To retrieve using PowerShell, use the Get-ScheduledTask command.
How to get the list of scheduled tasks? Just run Schtasks command and you can see the list of scheduled commands. We can delete a schedule task using 'schtasks /delete /TN task_name' command.
1 Answer. open Event Viewer and navigate to Applications and Services Logs / Microsoft / Windows / TaskScheduler / Optional, you will see all the Task Histories.
Look at the link I gave you in my last answer. The link for SCHTASKS.exe.
Look at the section called Querying for Task Information.
Here is my code to check the current running status. You can play with the output to modify this for your needs.
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "SCHTASKS.exe";
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.Arguments = String.Format("/Query /S {0} /TN {1} /FO TABLE /NH", MachineName, ScheduledTaskName);
p.Start();
// Read the error stream
string error = p.StandardError.ReadToEnd();
//Read the output string
p.StandardOutput.ReadLine();
string tbl = p.StandardOutput.ReadToEnd();
//Then wait for it to finish
p.WaitForExit();
//Check for an error
if (!String.IsNullOrWhiteSpace(error))
{
throw new Exception(error);
}
//Parse output
return tbl.Split(new String[] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries)[1].Trim().EndsWith("Running");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With