我想使用 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-ScheduledTask
中Export-ScheduledTask
,如下面的代码片段所示:
Get-ScheduledTask -TaskPath $taskpath |
Foreach-Object { $_ | Export-ScheduledTask |
Out-File (Join-Path $savefolder "$($_.TaskName).xml") }