Update Scheduled Task in Sub Folder (The Specified

2019-07-24 05:14发布

问题:

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?

回答1:

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" 
}