我只发现了一个实际需要这样做的案例,就像加里提到的那样,我用一些逻辑来包装它,以避免连续重启。
我们遇到了这样的情况,即“新创建的”服务器有一些挂起的文件重命名,即使多次重新启动也从未真正消失过,因此如果我们运行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