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

懵懂的女人 提交于 2019-12-11 08:17:41

问题


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


来源:https://stackoverflow.com/questions/40001457/update-scheduled-task-in-sub-folder-the-specified-path-is-invalid-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!