我应该在BoxStarter脚本中包含reboot命令吗?

时间:2015-03-12 13:51:20

标签: powershell restart reboot chocolatey boxstarter

问题

在声明if (Test-PendingReboot) { Invoke-Reboot }时,是否有理由在BoxStarter脚本中包含$Boxstarter.RebootOk=$true

背景

我最近发现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

2 个答案:

答案 0 :(得分:2)

就个人而言,我永远不会这样做,不。我依靠Boxstarter为我照顾这个,因为在内部它正在进行相同的检查,所以在我的脚本中另外做的是重复工作。

有些时候,正如你所提到的,你知道由于某些外部原因需要重新启动,所以我会直接调用Invoke-Reboot,但是这总是被一些guard子句包围以防止它每次发生,因为我一直希望我的脚本可重复。

答案 1 :(得分:0)

我只发现了一个实际需要的情况,就像Gary提到的那样,我把它包装成一些逻辑以避免连续重启。

我们遇到了#34;刚刚铸造的情况"服务器有一些挂起的文件重命名,即使多次重启也从未实际消失,所以如果我们运行Boxstarter,我们最终必须尽快杀死cmd窗口,如果我们可以在无限重启之间登录。

生成的脚本可以通过Install-BoxstarterPackage -DisableReboots <gistUrl>从一个要点运行,以清理你放入$ 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