简体   繁体   English

运行Powershell脚本,重新启动然后继续运行

[英]Running a Powershell script, restarting and then continue to run

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. 我只是从Powershell脚本编写的最基础开始,并且正在考虑按照一组指令创建脚本,然后重新启动并继续运行脚本的其余部分。

The first part of the script makes changes to the registry, firewall and ip/dns settings, then renames the server(win2012). 脚本的第一部分对注册表,防火墙和ip / dns设置进行更改,然后重命名服务器(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. 任何人都可以推荐一种非常简单的方法来实现重启和恢复。

The most easiest way is already built-in into Windows. 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. 有许多注册表项,您可以使用它们来配置一些在重新启动后将执行一次的操作。

For your use case, you probably want to use one of the RunOnce keys. 对于您的用例,您可能想要使用RunOnce键之一。 As always, the exhaustive documentation can be found in the MSDN pages, here's the essence of it: 与往常一样, 详尽的文档可以在MSDN页面中找到,其实质是:

[...] RunOnce registry keys cause programs to run each time that a user logs on. [...] RunOnce注册表项导致程序在用户每次登录时运行。 The data value for a key is a command line. 键的数据值为命令行。 Register programs to run by adding entries of the form description-string=commandline. 通过添加形式为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. 如果在一个特定的密钥下注册了多个程序,则这些程序的运行顺序不确定。

The Windows registry includes [...]: Windows注册表包括[...]:

  • HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce HKEY_LOCAL_MACHINE \\ SOFTWARE \\微软\\的Windows \\ CurrentVersion \\的RunOnce
  • HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce HKEY_CURRENT_USER \\软件\\微软\\的Windows \\ CurrentVersion \\的RunOnce

By default, the value of a RunOnce key is deleted before the command line is run. 默认情况下,在运行命令行之前会删除RunOnce键的值。 You can prefix a RunOnce value name with an exclamation point (!) to defer deletion of the value until after the command runs. 您可以在RunOnce值名称前添加一个感叹号(!),以将值的删除推迟到命令运行之后。 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. RunOnce键的值名称可以带有星号(*)前缀,以强制程序即使在安全模式下也可以运行。

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

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

Using RunOnce below HKLM would run the script for any user, but requires elevated rights to write the registry entry. 在HKLM以下使用RunOnce可以为任何用户运行该脚本,但是需要更高的权限才能写入注册表项。 In contrast, HKCU is bound to the current user, but does not require additional permissions. 相反,HKCU绑定到当前用户,但不需要其他权限。

For reboot, simply call the Windows shutdown command , eg 要重新启动,只需调用Windows shutdown命令 ,例如

shutdown /r 

要使用任务计划程序在重启/崩溃后自动恢复Powershell工作流,请参阅我的详细stackoverflow答案: https : //stackoverflow.com/a/31100397/1487851

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

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