运行 Powershell 脚本,重新启动然后继续运行 [英] Running a Powershell script, restarting and then continue to run

查看:235
本文介绍了运行 Powershell 脚本,重新启动然后继续运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习 Powershell 脚本的基础知识,正在考虑根据一组指令创建脚本,然后重新启动并继续运行脚本的其余部分.

I'm just starting out with the very basics of Powershell scripting and am looking at creating a script to one set of instructions then restart and continue running the rest of the script.

脚本的第一部分更改注册表、防火墙和 ip/dns 设置,然后重命名服务器(win2012).然后需要重新启动才能继续安装广告域服务和创建林.

The first part of the script makes changes to the registry, firewall and ip/dns settings, then renames the server(win2012). Then a restart is needed to continue with the installation of ad domain services and forest creation.

我环顾四周,但并不真正理解这些概念.任何人都可以推荐一种非常简单的方法来实现重启和恢复.

I've had a look around but don't really understand the concepts. Can anyone recommend a very easy way to implement the reboot and resume.

推荐答案

最简单的方法已经内置到 Windows 中.有一堆注册表项,您可以使用它们来配置一些要在重新启动后执行一次的操作.

The most easiest way is already built-in into Windows. There are a bunch of registry keys, which you can use to configure some action that is to be executed once after a reboot.

对于您的用例,您可能想要使用 RunOnce 键之一.与往常一样,详尽的文档可以可以在 MSDN 页面中找到,这里是它的本质:

For your use case, you probably want to use one of the RunOnce keys. As always, the exhaustive documentation can be found in the MSDN pages, here's the essence of it:

[...] RunOnce 注册表项会导致程序在用户每次登录时运行.键的数据值是一个命令行.通过添加 description-string=commandline 形式的条目来注册要运行的程序.您可以在一个键下写入多个条目.如果在某个特定键下注册了多个程序,则这些程序的运行顺序是不确定的.

[...] RunOnce registry keys cause programs to run each time that a user logs on. The data value for a key is a command line. Register programs to run by adding entries of the form description-string=commandline. You can write multiple entries under a key. If more than one program is registered under any particular key, the order in which those programs run is indeterminate.

Windows 注册表包括 [...]:

The Windows registry includes [...]:

  • HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
  • HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce

默认情况下,RunOnce 键的值在命令行运行之前被删除.您可以使用感叹号 (!) 作为 RunOnce 值名称的前缀,以将值的删除推迟到命令运行之后.如果没有感叹号前缀,如果 RunOnce 操作失败,则下次启动计算机时不会要求运行关联的程序.

By default, the value of a RunOnce key is deleted before the command line is run. You can prefix a RunOnce value name with an exclamation point (!) to defer deletion of the value until after the command runs. Without the exclamation point prefix, if the RunOnce operation fails the associated program will not be asked to run the next time you start the computer.

默认情况下,当计算机以安全模式启动时,这些键会被忽略.RunOnce 键的值名称可以以星号 (*) 为前缀,以强制程序即使在安全模式下也能运行.

By default, these keys are ignored when the computer is started in Safe Mode. The value name of RunOnce keys can be prefixed with an asterisk (*) to force the program to run even in Safe mode.

因此,基本上您唯一需要做的就是在该 reg 键下创建一个条目,该条目调用 powershell 并将您的脚本作为参数传递.

So basically the only thing you need to do is to create an entry under that reg key which calls powershell and passes your script as argument.

set-location HKCU:\Software\Microsoft\Windows\CurrentVersion\RunOnce
new-itemproperty . MyKey -propertytype String -value "Powershell c:\temp\myscript.ps1"

在 HKLM 下使用 RunOnce 可为任何用户运行脚本,但需要提升权限才能写入注册表项.相比之下,HKCU绑定了当前用户,但不需要额外的权限.

Using RunOnce below HKLM would run the script for any user, but requires elevated rights to write the registry entry. In contrast, HKCU is bound to the current user, but does not require additional permissions.

要重新启动,只需调用 Windows shutdown命令,例如

For reboot, simply call the Windows shutdown command, e.g.

shutdown /r 

这篇关于运行 Powershell 脚本,重新启动然后继续运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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