Update Scheduled Task in Sub Folder (The Specified path is invalid.) PowerShell 4.0
Asked Answered
C

1

1

When running the script below I get the following error: Set-ScheduledTask : The specified path is invalid.

$Action = New-ScheduledTaskAction -Execute """C:\Program Files\Sync\Sync.exe""" -Argument "C:\ProgramData\Sync\Script.bat"
Set-ScheduledTask  -TaskName "Task Name" -TaskPath "\SFTP Schedules\Non-Live\" -Action $Action

This is the folder structure.

Task Scheduler Folders

Anyone got any idea why?

Candancecandela answered 12/10, 2016 at 14:38 Comment(1)
I have done some testing and I think I am unable to change these tasks because of 2 reasons. A) I am not the Author B) I am not the run as user. I can get round point B by feeding in the user and password into the script as well but I don't know how to get round the fact I am not the Author.Candancecandela
P
0

Your code is fine as long as your account is in the administrator group on the server, you should be able to change the Task even if your not the author. Also make sure that you are running your PowerShell console as Administrator.

But if you are not the runAs user then you will need to supply the credentials of that user to be able to edit the Task.

Set-ScheduledTask -Password "password" -User "Domain\User" -TaskName "name" -TaskPath ... 

If you want to export all the task in a scheduled task folder to XML and then replace the author in the XML you can you the following code. Change $_.TaskPath -eq '\' to match the folder you want to export.

$XMLDestFolder = "C:\XML\"
Get-ScheduledTask | ?{$_.TaskPath -eq '\'} | %{
    $TaskXML = Export-ScheduledTask -TaskName $_.TaskName
    $TaskXML -replace "(?<=<Author>).*?(?=</Author>)","ADFP\NETMANFP" > "$XMLDestFolder$($_.TaskName).xml" 
} 
Psychodynamics answered 13/10, 2016 at 9:13 Comment(5)
Hi @Richard, I am local admin on the server, I have full NTFS permissions to the file in sys32\tasks, I am running Powershell in Admin mode and I have added the run as user\password commands. It still says the path is invalid when trying to amend a task that has someone else as the author!!Candancecandela
What if you just delete the one your not author of and then recreate it exactly same but with you as the author?Psychodynamics
That is an option, but I have 178 scheduled tasks which I need to update the Action on :( Hence why I am trying to get a script which will do it for me.Candancecandela
Could you export them all to xml and then import them all from the xml files. I don't think it would be to hard to write a script to do that, and that might change all the authors.Psychodynamics
I unfortunately that doesn't amend the author as it is written to the XML. I am currently looking at another product which will allow me to export them, amend the XML element values the import them again. This is a backup if I cannot get past this powershell issue. I am not giving up yet.Candancecandela

© 2022 - 2024 — McMap. All rights reserved.