简体   繁体   English

SQL 在任务计划程序中使用 powershell 进行服务器备份

[英]SQL Server backup using powershell in Task scheduler

I am using PowerShell to backup a SQL Server database and use Windows task scheduler to schedule the script我正在使用 PowerShell 备份 SQL 服务器数据库并使用 Windows 任务计划程序来安排脚本

Test测试

  1. Run the script manually, it works.手动运行脚本,它有效。
    Both SQL Server backup file ( test.bak ) and test file ( HelloWorld.txt ) were created. SQL 服务器备份文件 ( test.bak ) 和测试文件 ( HelloWorld.txt ) 均已创建。

  2. Run via Windows task scheduler, only HelloWorld.txt was created, the SQL Server backup file was not created.通过 Windows 任务调度程序运行,只创建了HelloWorld.txt ,没有创建 SQL 服务器备份文件。

So at least;至少如此;

  1. The PowerShell script did run. PowerShell 脚本确实运行了。
  2. Credentials were ok for the manual run凭据可以手动运行

Problem问题

Why is the SQL SErver backup file not created when run from Windows task scheduler?为什么从 Windows 任务计划程序运行时没有创建 SQL SErver 备份文件?

Here is my SQLbackup.ps1 file:这是我的SQLbackup.ps1文件:

$credential = import-clixml -path c:\pp\test\sqlcred.xml
Backup-SqlDatabase -ServerInstance Server264\SUPPORT2K19 -Database PPTest -BackupAction Database -BackupFile "C:\PP\Test\Test.bak" -Credential $credential

Set-Content -Path "C:\PP\Test\HelloWorld.txt" -Value "Hello World"

Task scheduler settings are:任务计划程序设置为:

  • User is SYSTEM,用户是SYSTEM,
  • Select run whether user is logged on on or not Select 无论用户是否登录都运行
  • Tick run with highest privilege Tick 以最高权限运行

In action tab:在操作选项卡中:

  • Program: powershell程序:powershell
  • Add arguments: -executionpolicy bypass -file C:\PP\Test\sqlbackup.ps1添加arguments: -executionpolicy bypass -file C:\PP\Test\sqlbackup.ps1

Any suggestion why the SQL Server backup file was not created?为什么没有创建 SQL 服务器备份文件的任何建议?

I think the cmdlet Backup-SqlDatabase produce an error, and you do not have a mechanism to capture that.我认为 cmdlet Backup-SqlDatabase会产生错误,而您没有捕获该错误的机制。

Try implementing try/catch to capture the error and then you can do what you want with that information, for example:尝试实现try/catch来捕获错误,然后您可以使用该信息执行您想要的操作,例如:

$credential = import-clixml -path c:\pp\test\sqlcred.xml
try {
    Backup-SqlDatabase -ServerInstance Server264\SUPPORT2K19 -Database PPTest -BackupAction Database -BackupFile "C:\PP\Test\Test.bak" -Credential $credential -ErrorAction Stop
}
catch {
    Set-Content -Path "C:\PP\Test\Error.txt" -Value "$_"
}
Set-Content -Path "C:\PP\Test\HelloWorld.txt" -Value "Hello World"

This would capture the potential error and write it in a Error.txt file, same way as the HelloWorld.txt .这将捕获潜在的error并将其写入Error.txt文件,与HelloWorld.txt相同。

Important part is that Try/Catch capture only terminating errors, so we add the -ErrorAction Stop at the end of the cmdlet to make any error terminating so we can execute the script in the catch script block.重要的部分是Try/Catch仅捕获终止错误,因此我们在 cmdlet 的末尾添加-ErrorAction Stop以使任何错误终止,以便我们可以在catch脚本块中执行脚本。

You can read more about it in the Microsoft docs: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_try_catch_finally?view=powershell-7.2您可以在 Microsoft 文档中阅读更多相关信息: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_try_catch_finally?view=powershell-7.2

PS The documentation is for Powershell 7.2 PS 该文档适用于 Powershell 7.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 powershell 和任务计划程序恢复 MS SQL 服务器数据库 - Restore MS SQL Server Database using powershell and task scheduler 使用Windows Task Scheduler运行的PowerShell脚本未运行SQL Server SSIS DTEXEC.EXE作业 - PowerShell script run using Windows Task Scheduler is not running a SQL Server SSIS DTEXEC.EXE job 如何使用Powershell为SQL Server备份指定备份目录 - How to specify backup directory for SQL Server backup using powershell 使用Powershell远程备份SQL Server Docker容器 - Remotely backup SQL Server Docker container Using Powershell Powershell SQL Server 存储过程备份 - Powershell SQL Server stored procedure backup Powershell更改SQL Server的备份脚本 - Powershell alter Backup Scripts for SQL Server 使用Powershell获取SQL备份数据 - Using Powershell to get SQL Backup Data 使用 MS 任务计划程序每 15 分钟自动将数据从 Excel 导入 SQL 服务器 - Import data from Excel to SQL Server automatically for every 15 min of time using MS Task Scheduler 使用Windows Task Scheduler自动执行每天运行的作业时,哪个用户将调用SQL Server代理作业? - Which User will be invoking SQL Server Agent Job when using a Windows Task Scheduler to automate the Job to run daily? 如何使用 Powershell 压缩 SQL Server 备份文件并保存在同一目录中? - How do I zip a SQL Server backup file using Powershell and save in the same directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM