如何使用 PowerShell 導出計劃任務

Jacki

我們喜歡將所有計劃任務遷移到另一台服務器或計算機上。與其在任務計劃程序中單獨導出計劃任務,不如運行 PowerShell 腳本為您批量導出所有任務。運行腳本並保存計劃任務以進行備份也非常好。在本文中,您將學習如何使用 PowerShell 導出計劃任務。

在任務計劃程序中導出計劃任務

無法從任務計劃程序程序導出多個或所有計劃任務。只能導出單個任務計劃。

選擇任務計劃程序中的所有任務,沒有導出的選項。

在任務計劃程序中選擇單個任務,您可以導出該任務。

如果您有很多計劃任務,這可能會非常耗時。那麼,更好的方法是什麼? PowerShell 來救援。

要導出所有計劃任務,我們將使用獲取計劃任務用於檢索它們的 PowerShell cmdlet 和導出計劃任務用於導出它們的 PowerShell cmdlet。

讓我們在下一步中研究一下。

下載 Export-SchedTasks PowerShell 腳本

上創建兩個文件夾(中:)駕駛:

參見:防止在 Windows 11 中創建新計劃任務的行之有效的方法

  • 腳本
  • 任務

下載 Export-SchedTasks.ps1 PowerShell 腳本並將其放置在C:腳本文件夾。該腳本會將所有計劃任務導出到C:任務文件夾。

筆記:它不會導出包含計劃任務的 Microsoft 文件夾,因為它們是默認任務。

確保文件未被阻止,以防止運行腳本時出現錯誤。請閱讀文章運行 PowerShell 腳本時出現未數字簽名錯誤來了解更多信息。

另一種選擇是將以下代碼複製並粘貼到記事本中。給它起個名字導出-SchedTasks.ps1並將其放置在C:腳本文件夾。

<#
    .SYNOPSIS
    Export-SchedTasks.ps1

    .DESCRIPTION
    Export Windows Scheduled Tasks on Windows Server and Windows Clients for backup purposes.

    .LINK
    www.alitajran.com/export-scheduled-tasks-powershell/

    .NOTES
    Written by: ALI TAJRAN
    Website:    www.alitajran.com
    LinkedIn:   linkedin.com/in/alitajran

    .CHANGELOG
    V1.00, 12/08/2023 - Initial version
#>

# Define the backup path
$backupPath = "C:Tasks"

# Get the unique task folders from the scheduled tasks
$taskFolders = (Get-ScheduledTask).TaskPath | Where-Object { ($_ -notmatch "Microsoft") } | Select-Object -Unique

# Start exporting of scheduled tasks
Write-Host "Start exporting of scheduled tasks." -ForegroundColor Cyan

# Check if the backup path exists
if (Test-Path -Path $backupPath) {
    Write-Host "Folder already exists: $backupPath" -ForegroundColor Yellow
}
else {
    # Create the backup path if it doesn't exist
    New-Item -ItemType Directory -Path $backupPath | Out-Null
    Write-Host "Backup path created: $backupPath" -ForegroundColor Green
}

# Loop through each task folder
foreach ($taskFolder in $taskFolders) {
    Write-Host "Task folder: $taskFolder" -ForegroundColor Cyan

    # Check if the task folder is not the root folder
    if ($taskFolder -ne "") {
        $folderPath = "$backupPath$taskFolder"

        # Create the task folder in the backup path if it doesn't exist
        if (-not (Test-Path -Path $folderPath)) {
            New-Item -ItemType Directory -Path $folderPath | Out-Null
        }
        else {
            Write-Host "Folder already exists: $folderPath" -ForegroundColor Yellow
        }
    }

    # Get the tasks in the task folder
    $tasks = Get-ScheduledTask -TaskPath $taskFolder -ErrorAction SilentlyContinue

    # Loop through each task in the task folder
    foreach ($task in $tasks) {
        $taskName = $task.TaskName

        # Export the task and save it to a file
        $taskInfo = Export-ScheduledTask -TaskName $taskName -TaskPath $taskFolder
        $taskInfo | Out-File "$backupPath$taskFolder$taskName.xml"
        Write-Host "Saved file $backupPath$taskFolder$taskName.xml" -ForegroundColor Cyan
    }
}

# Exporting of scheduled tasks finished
Write-Host "Exporting of scheduled tasks finished." -ForegroundColor Green

這就是它的樣子。

運行導出計劃任務 PowerShell 腳本

運行 Export-SchedTasks.ps1 PowerShell 腳本以獲取所有任務計劃任務並將它們導出到C:任務文件夾。

C:scripts.Export-SchedTasks.ps1

輸出顯示:

Start exporting of scheduled tasks.
Backup path created: C:Tasks
Task folder: 
Saved file C:TasksCreateExplorerShellUnelevatedTask.xml
Saved file C:TasksMicrosoftEdgeUpdateTaskMachineCore{68B94FCC-61AA-45EA-B214-C666C5A7C344}.xml
Saved file C:TasksMicrosoftEdgeUpdateTaskMachineUA{B5BEDAA1-3440-4D7D-A459-0A8C98600F11}.xml
Saved file C:TasksUser_Feed_Synchronization-{D86918D5-2FFC-4B1D-9AF1-0B66C68F64B2}.xml
Saved file C:Taskswin-acme renew (acme-v02.api.letsencrypt.org).xml
Task folder: Mozilla
Saved file C:TasksMozillaFirefox Background Update 308046B0AF4A39CB.xml
Saved file C:TasksMozillaFirefox Default Browser Agent 308046B0AF4A39CB.xml
Exporting of scheduled tasks finished.

驗證計劃任務 XML 文件

Export-SchedTasks.ps1 PowerShell 腳本將所有任務批量導出到 XML 文件。找到路徑中的XML文件C:任務

使用您喜歡的應用程序打開 XML 文件。例如,Microsoft Edge、Notepad 或 Notepad++。

XML 文件看起來很棒。

這是否有助於您使用 PowerShell 將計劃任務備份到 XML 文件?

結論

您學習瞭如何使用 PowerShell 導出計劃任務。首先,運行 Export-SchedTasks PowerShell 腳本。接下來,進入導出文件夾,查看所有導出的XML文件中的計劃任務。該腳本適用於 Windows Server 和 Windows 客戶端。

您喜歡這篇文章嗎?您可能還喜歡 Windows Server 安裝後配置。不要忘記關注我們並分享這篇文章。