問題描述
服務器待重啟 (Server Pending Reboot)
我正在嘗試修改我的 PowerShell 腳本,以找到在我們的服務器上檢查 Pending Reboots 的最佳方法。此腳本檢查註冊表項。但是,我發現其他 PowerShell 腳本存在不一致,並希望獲得有關最佳方法的指導。
function PendingReboot ($comp) {
process {
try {
$WMI_OS = ""
$RegCon = ""
$WMI_OS = Get‑WmiObject ‑Class Win32_OperatingSystem ‑ComputerName $comp ‑ErrorAction Stop
if ($?){
try{
$RegCon = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]"LocalMachine",$comp)
If ($WMI_OS.BuildNumber ‑ge 6001){
$RegValueSetupex = ""
$RegValuePFRO2k8 = ""
$RegSubKeySM = $RegCon.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\")
$RegValueSetupex = $RegSubKeySM.GetValue("SetupExecute",$null)
if ($RegValueSetupex){
$RegValueSetupex = $true
}
$RegSubKeySM = $RegCon.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\")
$RegValuePFRO2k8 = $RegSubKeySM.GetValue("PendingFileRenameOperations",$null)
if ($RegValuePFRO2k8 ){
$RegValuePFRO2k8 = $true
}
$RegCon.Close()
if ( $RegValueSetupex ‑eq $true ‑or $RegValuePFRO2k8 ‑eq $true){
return '<font color="#FF0000">'+$true
}
else {
return $false
}
}
else{
$RegValuePFRO2k3 = $false;
$RegCon = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]"LocalMachine","$comp")
$RegSubKeySM = $RegCon.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\")
$RegValuePFRO2k3 = $RegSubKeySM.GetValue("PendingFileRenameOperations",$null)
$RegCon.Close()
If ($RegValuePFRO2k3) {
return '<font color="#FF0000">'+$true;
}
else {
return $false;
}
}
}
catch {
return '<font color="#FFFF00">'+"Remote Registry Service KO"
}
}
else {
throw $error[0].Exception
}
}
catch {
return '<font color="#FF0000">'+"RPC Issue"
}
}
}
參考解法
方法 1:
Try this.
function PendingBoot($comp) {
$pendingRebootTests = @(
@{
Name = 'RebootPending'
Test = { Get‑ItemProperty ‑Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing' Name 'RebootPending' ‑ErrorAction Ignore }
TestType = 'ValueExists'
}
@{
Name = 'RebootRequired'
Test = { Get‑ItemProperty ‑Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update' Name 'RebootRequired' ‑ErrorAction Ignore }
TestType = 'ValueExists'
}
@{
Name = 'PendingFileRenameOperations'
Test = { Get‑ItemProperty ‑Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager' ‑Name 'PendingFileRenameOperations' ‑ErrorAction Ignore }
TestType = 'NonNullValue'
}
)
$session = New‑PSSession ‑Computer SRV1
foreach ($test in $pendingRebootTests) {
$result = Invoke‑Command ‑Session $session ‑ScriptBlock $test.Test
if ($test.TestType ‑eq 'ValueExists' ‑and $result) {
$true
} elseif ($test.TestType ‑eq 'NonNullValue' ‑and $result ‑and $result.($test.Name)) {
$true
} else {
$false
}
}
$session | Remove‑PSSession
}
(by Josh、Daniel Björk)