Author Posts

October 7, 2015 at 7:02 am

Hello, I'm trying to change scheduled tasks through powershell.

The condtion must be that the trigger must not have a full path..such as..

function get-tasks ($folders)
{
	foreach($folder in $folders)
	{
			try
			{
				foreach ($task in $folder.gettasks(""))
				{
					if(($task.xml -notmatch '.*\\.*') -and $task.xml -notmatch "" )
					{	
					$task.name
					}
				}
				get-tasks ($folder.getfolders(""))
			}
			catch
			{
				get-tasks ($folder.getfolders(""))	
			}	
	}
	
}	



#creates an object for task sched
$sched = New-Object -ComObject schedule.service
$sched.connect("Localhost") 

#starts the function to scan
$schedissues = @(get-tasks $sched.getfolder(""))

Where $schedissues would return, say: UPnPHostConfig

I would then drill down on that task

[xml]$x = get-content "C:\Windows\System32\Tasks\Microsoft\Windows\UPnP\UPnPHostConfig"


#$z = sc.exe
$x.Task.Actions.Exec.Command = $z

$x.Task.Actions.Exec.Command = "%SystemRoot%\system32\$z"

But it seems if I change the xml through powershell in this fashion it corrupts the task.

Is there a way to achieve this? Would it be easier to save the task's XML and delete the task and reimport it once it's changed?

I can only use V2 of powershell for these tasks, unfortunately.

October 7, 2015 at 11:51 am

It's possible there's some funkiness in the XML that makes the standard .NET parser not treat it the way Task Scheduler wants. Unfortunately, if you're stuck on that old a version of PowerShell, you may not have any good workarounds. I'd probably try the export/delete/import approach and see what that does. It'd be worth examining the XML before and after to see if there are any obvious, significant-looking differences.

October 12, 2015 at 5:44 am

Sorry for the late reply, went on vacation last week...

It should also be noted that if i edit the XML file in notepad in c:\windows\system32\tasks it gives also gives me a 'Task * THe task image is corrupt or has been tampered with.' when I open task manager.

I might have to go with the export/import method. Is there a way to do that through powershell v2 or will I have to kick it off with schtasks.exe?