当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何使用 PowerShell 导出计划任务

作者:精品下载站 日期:2024-12-14 06:31:19 浏览:15 分类:玩电脑

如何使用 PowerShell 导出计划任务


我们喜欢将所有计划任务迁移到另一台服务器或计算机上。与其在任务计划程序中单独导出计划任务,不如运行 PowerShell 脚本为您批量导出所有任务。运行脚本并保存计划任务以进行备份也非常好。在本文中,您将学习如何使用 PowerShell 导出计划任务。

在任务计划程序中导出计划任务

无法从任务计划程序程序导出多个或所有计划任务。只能导出单个任务计划。

选择任务计划程序中的所有任务,没有导出的选项。

[玩转系统] 如何使用 PowerShell 导出计划任务

在任务计划程序中选择单个任务,您可以导出该任务。

[玩转系统] 如何使用 PowerShell 导出计划任务

如果您有很多计划任务,这可能会非常耗时。那么,更好的方法是什么? PowerShell 来救援。

使用 PowerShell 导出所有计划任务

要导出所有计划任务,我们将使用 Get-ScheduledTask PowerShell cmdlet 来检索它们,并使用 Export-ScheduledTask PowerShell cmdlet 来导出它们。

让我们在下一步中研究一下。

下载 Export-SchedTasks PowerShell 脚本

(C:)驱动器上创建两个文件夹:

  • 脚本

  • 任务

下载 Export-SchedTasks.ps1 PowerShell 脚本并将其放置在 C:\Scripts 文件夹中。该脚本会将所有计划任务导出到 C:\Tasks 文件夹。

注意:它不会导出包含计划任务的 Microsoft 文件夹,因为它们是默认任务。

确保文件未被阻止,以防止运行脚本时出现错误。请阅读文章运行 PowerShell 脚本时出现未数字签名错误来了解更多信息。

另一种选择是将以下代码复制并粘贴到记事本中。将其命名为 Export-SchedTasks.ps1 并将其放置在 C:\Scripts 文件夹中。

<#
    .SYNOPSIS
    Export-SchedTasks.ps1

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

    .LINK
    www.a-d.site/export-scheduled-tasks-powershell/

    .NOTES
    Written by: ALI TAJRAN
    Website:    www.a-d.site
    LinkedIn:   linkedin.com/in/a-d

    .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 导出计划任务

运行导出计划任务 PowerShell 脚本

运行 Export-SchedTasks.ps1 PowerShell 脚本以获取所有任务计划任务并将它们导出到 C:\Tasks 文件夹中的 XML 文件。

C:\scripts\.\Export-SchedTasks.ps1

输出显示:

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

验证计划任务 XML 文件

Export-SchedTasks.ps1 PowerShell 脚本将所有任务批量导出到 XML 文件。在路径 C:\Tasks 中找到 XML 文件。

[玩转系统] 如何使用 PowerShell 导出计划任务

使用您喜欢的应用程序打开 XML 文件。例如,Microsoft Edge、Notepad 或 Notepad++。

[玩转系统] 如何使用 PowerShell 导出计划任务

XML 文件看起来很棒。

这是否有助于您使用 PowerShell 将计划任务备份到 XML 文件?

阅读更多内容:使用 PowerShell 创建计划任务 »

结论

您学习了如何使用 PowerShell 导出计划任务。首先,运行 Export-SchedTasks PowerShell 脚本。接下来,进入导出文件夹,查看所有导出的XML文件中的计划任务。该脚本适用于 Windows Server 和 Windows 客户端。

您喜欢这篇文章吗?您可能还喜欢 Windows Server 安装后配置。不要忘记关注我们并分享这篇文章。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯