如果脚本运行正确 restart/run 下一部分

if script runs true restart/run next part

我正在尝试让脚本在脚本 returns $true 时重新启动计算机,我打算 运行 SCCM 或任务计划程序中的脚本。 如果它 returns $true 我打算让 SCCM 重新启动计算机但不知道如何做现在我试图在脚本本身中添加重新启动但不知道如何添加它正确.

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
}

If $true) {
  Restart-Computer

  }  Else {
  exit 
}  

If (Test-PendingReboot -eq "true") { 你的重启代码 }

此示例代码应该适合您。

function reboot
{
    function Get-property
    {
    try { 
            if (Get-ItemProperty 'hklm:software\microsoft\windows\currentversion\policies\system' -name enablelua)
            {
                $value = "true"
            }
            else{ $value = "false" }
        }
    catch{}
    return $value
    }
    Get-property

    If (Get-property -eq "true") 
    { your code for restart }

}

reboot