【问题标题】:Should I ever include a reboot command in my BoxStarter script?我应该在我的 BoxStarter 脚本中包含重启命令吗?
【发布时间】:2015-03-12 13:51:20
【问题描述】:
问题
当声明 $Boxstarter.RebootOk=$true 时,是否有理由在 BoxStarter 脚本中包含 if (Test-PendingReboot) { Invoke-Reboot }?
背景
我最近发现了 BoxStarter,并注意到许多脚本包含以下代码:if (Test-PendingReboot) { Invoke-Reboot }。
这包括具有以下选项的脚本:$Boxstarter.RebootOk=$true 和 $Boxstarter.AutoLogin=$true;即允许重新启动并根据需要继续。
在BoxStarter site 上作出以下声明:
Boxstarter 拦截所有 Chocolatey 安装命令并检查
待重启。如果检测到挂起的重启,Boxstarter 将
重新启动机器并自动重新登录用户并恢复
安装。
注意:我知道在进行不会更新 PendingReboot 标志的更改后,有时可能需要Invoke-Reboot;例如使某些注册表更改生效;我的问题纯粹与包装在if (Test-PendingReboot) 语句中时使用此命令有关。
更新:也在 Google 网上论坛上提问:https://groups.google.com/forum/#!topic/boxstarter/D0kiRqJyiCY
【问题讨论】:
标签:
powershell
restart
reboot
chocolatey
boxstarter
【解决方案1】:
就个人而言,我永远不会这样做,不。我依靠 Boxstarter 为我处理这个问题,因为它在内部进行了相同的检查,所以在我的脚本中添加它是重复工作。
正如您所提到的,有时您知道出于某种外部原因需要重新启动,所以我会直接调用 Invoke-Reboot,但这总是会被一些保护子句包围,以防止每次都发生这种情况,因为我一直希望我的脚本是可重复的。
【解决方案2】:
我只发现了一种实际需要这样做的情况,就像 Gary 提到的那样,我将其包装在一些逻辑中以避免滚动连续重启。
我们遇到了这样一种情况:“新创建的”服务器有一些挂起的文件重命名,即使多次重新启动也从未真正消失,所以如果我们运行 Boxstarter,我们最终不得不尽可能快地终止 cmd 窗口在无限重启之间登录。
生成的脚本可以通过Install-BoxstarterPackage -DisableReboots <gistUrl> 从 gist 运行,以清理您放入 $badFile 的任何文件(您可以列出一个列表)。
此脚本的一个警告是它需要交互式提示登录凭据。如果您信任您的系统和网络,您可以使用纯文本密码并组装一个凭证,我认为这是最坏的情况。
抱歉,这似乎破坏了语法荧光笔。
Import-Module $env:appdata\Boxstarter\Boxstarter.Common
$badSpoolReg = '\??\C:\Windows\system32\spool\PRTPROCS\x64\1_hpcpp130.dll'
$badSpoolFile = 'C:\Windows\system32\spool\PRTPROCS\x64\1_hpcpp130.dll'
# Next bits taken from the 'Get-PendingReboot' module on the Script Gallery.
$Computer = $env:COMPUTERNAME
$HKLM = [UInt32] "0x80000002"
$WMI_Reg = [WMIClass] "\\$Computer\root\default:StdRegProv"
## Query PendingFileRenameOperations from the registry
$RegSubKeySM = $WMI_Reg.GetMultiStringValue($HKLM,"SYSTEM\CurrentControlSet\Control\Session Manager\","PendingFileRenameOperations")
#$RegSubKeySM # Debug print of the list if you want to run by hand
$RegValuePFRO = $RegSubKeySM.sValue | where { $_ } # Ignore empty values
#$RegValuePFRO # Debug print of the list if you want to run by hand
# Credential is required for Create-BoxstarterTask
# Create-BoxstarterTask required to call Invoke-FromTask
# see https://github.com/mwrock/boxstarter/issues/121
$cred = Get-Credential
Create-BoxstarterTask $cred
# Perhaps could be improved using set membership comparison?
# like (if $badSpoolReg in $RegValuePFRO.Values?)
foreach ($path in $RegValuePFRO) {
if ($path -contains $badSpoolReg) {
write-output "Bogey on my six!"
Get-Service spooler | stop-service
Invoke-FromTask "rm -fo $badSpoolFile" # Files in "protected" paths require extra work to remove
$Boxstarter.RebootOk = $true # Need to enable this to allow Invoke-Reboot to work
Write-output "Took out the bogey, resetting system state"
Invoke-Reboot # Manually called but within a fairly good gate
} else {
write-output "No bogeys sighted Captain!"
}
}
Remove-BoxstarterTask