[玩转系统] 如何使用 PowerShell 导出计划任务
作者:精品下载站 日期:2024-12-14 06:31:19 浏览:15 分类:玩电脑
如何使用 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 脚本
运行 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 文件。
使用您喜欢的应用程序打开 XML 文件。例如,Microsoft Edge、Notepad 或 Notepad++。
XML 文件看起来很棒。
这是否有助于您使用 PowerShell 将计划任务备份到 XML 文件?
阅读更多内容:使用 PowerShell 创建计划任务 »
结论
您学习了如何使用 PowerShell 导出计划任务。首先,运行 Export-SchedTasks PowerShell 脚本。接下来,进入导出文件夹,查看所有导出的XML文件中的计划任务。该脚本适用于 Windows Server 和 Windows 客户端。
您喜欢这篇文章吗?您可能还喜欢 Windows Server 安装后配置。不要忘记关注我们并分享这篇文章。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag