【问题标题】:How can I check for a pending reboot?如何检查挂起的重新启动?
【发布时间】:2017-12-18 11:53:30
【问题描述】:

我试图了解 Windows 机器在哪里需要或不需要重新启动。但是,我的脚本抛出错误。

powershell "$key = Get-Item "HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -ErrorAction SilentlyContinue"

Error :
Get-Item : A positional parameter cannot be found that accepts argument
'Update\RebootRequired'.
At line:1 char:8
+ $key = Get-Item
HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Aut ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-Item], ParameterBindin
   gException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell
   .Commands.GetItemCommand

我在“命令提示符”中运行此命令。不知道是什么意思!

【问题讨论】:

  • 嘿,得到了答案 powershell "(Invoke-WmiMethod -Namespace root\ccm\clientsdk -Class CCM_ClientUtilities -Name determineIfRebootPending).RebootPending"
  • 回答您自己的问题甚至接受它是完全可以接受的......考虑将您的评论添加为正确的答案 - 它可能会在未来帮助其他人。

标签: powershell installation automation registry


【解决方案1】:

等待重新启动可能由多种原因引起,而不仅仅是其他答案中详述的原因。试试 PendingReboot 模块,它将各种测试合并到一个 cmdlet 中:

# Install
Install-Module -Name PendingReboot

# Run
Test-PendingReboot -Detailed

【讨论】:

  • 这给了我足够的信息来了解重启请求的原因,这也是我一直在寻找的。​​span>
【解决方案2】:

您需要检查 2 个路径,一个键,您需要通过 WMI 查询配置管理器以检查所有可能的位置。

#Adapted from https://gist.github.com/altrive/5329377
#Based on <http://gallery.technet.microsoft.com/scriptcenter/Get-PendingReboot-Query-bdb79542>
function Test-PendingReboot {
    if (Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -EA Ignore) { return $true }
    if (Get-Item "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -EA Ignore) { return $true }
    if (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -EA Ignore) { return $true }
    try { 
        $util = [wmiclass]"\\.\root\ccm\clientsdk:CCM_ClientUtilities"
        $status = $util.DetermineIfRebootPending()
        if (($status -ne $null) -and $status.RebootPending) {
            return $true
        }
    }
    catch { }

    return $false
}

Test-PendingReboot

【讨论】:

【解决方案3】:

您的语法不正确,如果您想从 cmd 运行 PowerShell 命令,它必须如下所示:

powershell.exe "Get-Item 'HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired'"

但就像 Mathis 提到的那样,只有在等待重启时才存在此密钥。

【讨论】:

  • 3 号。应该是问题中的评论。这不是答案的一部分。
  • @Matt 是的......我刚刚注意到你的评论(我知道我很快......:D)。我重新安排了答案,感谢您的提醒。
【解决方案4】:

我发现导致这种情况的一件事(我的头疼不止于此)是每次我尝试运行 SCCM 1906 更新时,由于等待重启而失败。在我的调查中使用这个脚本,我注意到似乎是 ComponentBasedServicing 阻止了工作,这是自动安装的可选组件。多一点挖掘将我带到一个名为 LanguageComponentsInstaller 的计划任务。我禁用了这个,我一直在关注它,但它似乎已经解决了这个问题。

感谢脚本。尝试破解这个鸡蛋为我节省了很多压力:)

【讨论】:

    【解决方案5】:

    (我更愿意将此作为对已接受答案的评论添加,但代码不合适。)

    我认为以下功能可以消除一些不必要的重启。 PendingFileRenameOperations 注册表项不仅支持重命名,还支持删除(本质上表示为“重命名为 null”)*。我所做的假设是删除表示在此期间不会影响功能的待处理清理操作。

    <#
    .SYNOPSIS
    Returns true if any true renames-- deletes are ignored-- are present in the
    PendingFileRenameOperations registry key.
    #>
    function Test-PendingFileRename {
        [OutputType('bool')]
        [CmdletBinding()]
        param()
        $operations = (Get-Item 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\').GetValue('PendingFileRenameOperations')
        if ($null -eq $operations) {
            $false
        } else {
            $trueOperationsCount = $operations.Length / 2
            $trueRenames = [System.Collections.Generic.Dictionary[string, string]]::new($trueOperationsCount)
            for ($i = 0; $i -ne $trueOperationsCount; $i++) {
                $operationSource = $operations[$i * 2]
                $operationDestination = $operations[$i * 2 + 1]
                if ($operationDestination.Length -eq 0) {
                    Write-Verbose "Ignoring pending file delete '$operationSource'"
                } else {
                    Write-Host "Found a true pending file rename (as opposed to delete). Source '$operationSource'; Dest '$operationDestination'"
                    $trueRenames[$operationSource] = $operationDestination
                }
            }
            $trueRenames.Count -gt 0
        }
    }
    

    人们可以通过在顶部插入上述函数然后替换该行来在接受的答案的脚本中实现这一点

    if (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -EA Ignore) { return $true }
    

    if (Test-PendingFileRename) { return $true }
    

    * 参考:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-25
      • 2019-05-20
      • 1970-01-01
      • 2013-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多