每当启动服务器重启时运行 powershell 脚本并在 cmd 中显示脚本进度 [英] Run powershell script whenever a server reboot is initiated and show the script progress in cmd

查看:19
本文介绍了每当启动服务器重启时运行 powershell 脚本并在 cmd 中显示脚本进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当服务器重新启动/关闭(无论是正常的还是可耻的重新启动),我都需要运行一个 powershell 脚本.脚本会每隔1分钟停止4个应用服务,最后重启系统.(这是业务需求,不要问为什么)

I need to run a powershell script whenever the server is rebooted/shutdown (whether graceful or disgraceful reboot). The script will stop 4 application services at an interval of 1 minute and then finally reboots the system.(This is a business requirement, don't ask why)

如何让服务器在启动重启或关闭时调用 .ps1 脚本.

How can I make server to invoke the .ps1 script whenever a reboot or a shutdown is initiated.

我的测试结果:我尝试创建一个测试脚本,该脚本将生成一个具有当前日期/时间的文本文件,并将其添加到事件日志 6006 触发器上的计划任务(每当系统重新启动/关闭时都会创建该脚本.)我选中了以最高权限运行"框,但在系统重新启动后,没有按预期生成文本文件,尽管它在手动运行时生成.

My test results: I tried to create a test script which will generate a text file with current date/time and added it to the scheduled task on the trigger of event log 6006 (which is created whenever a system reboot/shutdown is initiated.) I checked the box -"Run with highest privileges" but after system restart no text file was generated as it was supposed to, although it generates when ran manually.

我们是否有更好的方法来实现这一点?

Do we have any better approach to implement this?

(我的最终期望应该是这样的——在一个随机的一天,当一个命令提示符窗口在他面前打开并显示如下消息时,随机用户在每月补丁后启动重启:

(My final expectation should look like this- On a random day a random user initiated reboot after a monthly patch when a command prompt window opens before him with message something like:

    Stopping service abc...
    Stopped.
    Waiting for 60 seconds.
    Stopping service xyz... 
    Stopped

编辑:按照 Kory 和 Alroc 的建议,通过将 .ps1 文件添加到 gpedit 中,我已经成功调用了 .ps1 文件,但该脚本仅在启动计算机重新启动时在后台运行.它不会打开常规的 cmd 窗口来显示进度.我也在下面添加 .ps1 脚本,它以 10 秒的间隔停止 2 个服务(为测试目的而选择),并且也会显示计时器,只有在手动运行时.当被关闭命令调用时,它将停止仅在后台提供服务,而不向用户显示进度.请协助实现这一目标?

EDIT: I've been successfully able to invoke the .ps1 file by adding it to the gpedit as suggested by Kory and Alroc but the script runs only in background when computer restart is initiated. It doesn't opens a regular cmd window to show the progress. I'm adding the .ps1 script as well below which stops 2 services(chosen for testing purpose) at an interval of 10 seconds and will show the timer as well, only when ran manually.When invoked by the shutdown command it'll stop services only in the background without showing the progress to the user. Kindly assist to achieve this?

 Write-Host "Shutdown script invoked"
    stop-service W32Time -force -PassThru
    for($i = 10 ; $i -gt 0 ; $i--)
    {
    Write-Progress -Activity "`n Waiting for" -status "`$i equals $i seconds"
    sleep 1
    }
    stop-service wuauserv -force -PassThru

推荐答案

经过深挖,我终于找到了如何在系统关闭时使 cmd 窗口可见.

After deep digging, I've finally figured out how to make the cmd window visible while system shutdown in progress.

以下是执行上述期望的完整步骤:

Here is the complete steps of performing above mentioned expectation:

  1. 打开 gpedit.msc
  2. 导航到计算机配置->Windows设置->脚本(启动/关机)->关机.
  3. 转到关机属性.在 powershell 脚本 标签中添加您的脚本并选择首先运行 Windows Powershell 脚本"
  1. Open gpedit.msc
  2. Navigate to Computer Configuration->Windows Settings->Scripts(Startup/Shutdown)->Shutdown.
  3. Go to Shutdown properties. In the powershell scripts tab add your script and select 'Run Windows Powershell script first'

以上步骤将在每次系统关闭时启用脚本调用.现在让脚本可见并显示进度:

Above steps will enable the invoke of script at every system shutdown. Now to make the script visible and show progress:

  1. 导航到计算机配置->管理模板->系统->脚本
  2. 在右侧窗格中显示的策略中,在下方启用属性:

  1. Navigate to Computer Configuration->Administrative Templates->System->Scripts
  2. Among the policies showing in the right pane enable below properties:

  • 首先在计算机启动、关闭时运行 Windows Powershell 脚本
  • 运行可见的关闭脚本

这篇关于每当启动服务器重启时运行 powershell 脚本并在 cmd 中显示脚本进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆