简体   繁体   English

系统重启后如何恢复PowerShell脚本?

[英]How to resume PowerShell script after system reboot?

I am using below powershell script to delete SharePoint Alerts. 我正在使用下面的powershell脚本删除SharePoint警报。

foreach ($site in Get-SPSite -Limit All) 
{ 
  "Site Collection $site" 
  foreach ($web in $site.allwebs)
  {
     "  Web $web"
     $c = $web.alerts.count
     "    Deleting $c alerts"
     for ($i=$c-1;$i -ge 0; $i--) { $web.alerts.delete($i) }
  }
}

There are around million alerts in each of Dev, Test and UAT environments. 在开发,测试和UAT环境中,大约有数百万条警报。 It takes many hours to delete all the alerts at one go and as the servers automatically get restarted periodically, the script doesn't get executed fully. 一次删除所有警报需要花费数小时,而且由于服务器会定期自动重新启动,因此脚本无法完全执行。

I am aware that to resume PowerShell scripts after reboot we can use PowerShell Workflow with Checkpoint-workflow but not sure where to place checkpoints and PSPersist . 我知道重启后要恢复PowerShell脚本,我们可以将PowerShell WorkflowCheckpoint-workflow一起使用,但不确定在哪里放置checkpoint和PSPersist

Need help to resume deleting of alerts in the above script after system reboot. 系统重启后,需要帮助以恢复删除上述脚本中的警报。

Update: After trying to implement it, I realized that SharePoint PowerShell cmdlets cannot be coupled with PowerShell Workflow. 更新:尝试实现它之后,我意识到SharePoint PowerShell cmdlet不能与PowerShell Workflow结合使用。 It doesn't allow 不允许

Add-PSSnapin "Microsoft.SharePoint.PowerShell"

to be added to workflows 被添加到工作流程中

Workflow SPAlerts
{
//Below tweaks didn't work
 InlineScript{Add-PSSnapin "Microsoft.SharePoint.PowerShell"}
 Invoke-Command -ScriptBlock {Add-PSSnapin "Microsoft.SharePoint.PowerShell"}
 Invoke-Expression "......"
}

The MSDN documentation states: MSDN文档指出:

You can place checkpoints anywhere in a workflow, including before and after each command or expression, or after each activity in a workflow. 您可以在工作流中的任何位置放置检查点,包括在每个命令或表达式之前和之后,或在工作流中的每个活动之后。 Windows PowerShell Workflow allows you to do so. Windows PowerShell工作流允许您这样做。

... Add a checkpoint after each significant part of the workflow completes; ...在工作流的每个重要部分完成之后添加检查点; a part that you do not want to repeat if the workflow is interrupted. 如果工作流程中断,您不想重复的部分。

... When the workflow definitions is complete, use the testing process to refine your checkpoints. ...当工作流程定义完成时,使用测试过程来完善您的检查点。 Add and remove (comment out) checkpoints to achieve both a reasonable running time and a reasonable recovery time. 添加和删​​除(注释掉)检查点以实现合理的运行时间和合理的恢复时间。

Looking at your code, I would place the checkpoint right after "Site Collection $site" if deleting alerts for the given website takes reasonable time on average. 查看您的代码,如果删除给定网站的警报平均花费合理的时间,我将在"Site Collection $site"之后放置检查点。 If there are just few sites each of them containing tons of alerts then I would place it on the start of the next foreach. 如果只有几个站点,每个站点包含大量警报,那么我会将其放在下一个foreach的开始处。

I would definitely not place it inside the worker foreach which deletes alerts. 我绝对不会将其放置在删除警报的worker foreach中。

I would also suggest you look at the foreach -Parallel capability of workflows to make the deleting parallel. 我还建议您查看工作流的foreach -Parallel功能,以使删除操作并行进行。 That way even the last site/website should get it's turn, even if the server is restarted often. 这样,即使服务器经常重启,即使是最后一个站点/网站也应该轮到它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM