使用 powershell 保存计划任务

使用 powershell 保存计划任务

我想使用 powershell 保存我的计划任务。

我尝试过这个:

$taskpath = "\mytasks\"   # all of my tasks are in this folder in Task Scheduler
$savefolder = "C:\tasks"  # where I want to save the xml files

Get-ScheduledTask -TaskPath $taskpath | foreach { Export-ScheduledTask -TaskName $_.TaskName | Out-File (Join-Path $savefolder "$($_.TaskName).xml") }

这些路径是存在的。

但是我收到此错误:Export-ScheduledTask : The system cannot find the file specified.

我究竟做错了什么?

答案1

您错过了TaskPath提供Export-ScheduledTask命令

-TaskPath [<String>]
    Specifies the path for a scheduled task in Task Scheduler namespace. You
     can use \ for the root folder. If you do not specify a path, the cmdlet
     uses the root folder.

使用

$taskpath = "\mytasks\"   # all of my tasks are in this folder in Task Scheduler
$savefolder = "C:\tasks"  # where I want to save the xml files

Get-ScheduledTask -TaskPath $taskpath | 
    Foreach-Object {
        $_.TaskName  ### debugging  output
        Export-ScheduledTask -TaskName $_.TaskName -TaskPath $_.TaskPath | 
                Out-File (Join-Path $savefolder "$($_.TaskName).xml") }

您无需指定特定的参数,TaskName而是TaskPath可以InputObject将从中获取的对象通过管道传输到 cmdletGet-ScheduledTaskExport-ScheduledTask,如下面的代码片段所示:

Get-ScheduledTask -TaskPath $taskpath | 
    Foreach-Object { $_ | Export-ScheduledTask | 
        Out-File (Join-Path $savefolder "$($_.TaskName).xml") }

相关内容