Powershell:检查计算机以查看是否需要重新启动,否则会跳过

问题描述 投票:-1回答:1

我正在尝试使用Test-Pendingreboot脚本来查找需要重新启动的计算机,但是如果$ true强制重新启动计算机,我不知道如何从Test-pendingreboot中获取结果。

任何帮助将不胜感激

谢谢

powershell restart reboot
1个回答
0
投票

我假设您正在使用在https://ilovepowershell.com/2015/09/10/how-to-check-if-a-server-needs-a-reboot/处找到的这段代码>

#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
}

您应该可以做一些简单的事情

由于尚不清楚您将如何使用它来查询机器,因此很难准确地回答您的问题。如果它正在检查本地主机,则可以:

$DoINeedAReboot = Test-PendingReboot
if ($DoINeedAReboot) { Restart-Computer }
© www.soinside.com 2019 - 2024. All rights reserved.