Powershell

使用 powershell 保存計劃任務

  • June 8, 2018

我想使用 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.

我究竟做錯了什麼?

您錯過了向TaskPathcmdletExport-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和參數,而是將獲取自 cmdlet 的對象通過TaskPath管道傳輸到cmdlet,如下面的程式碼片段所示:InputObject``Get-ScheduledTask``Export-ScheduledTask

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

引用自:https://serverfault.com/questions/915736