if script runs true restart/run next part

Deadly 提交于 2019-12-13 08:59:19

问题


I'm trying to get the script to restart the computer when the script returns $true, I'm planning to run the script in SCCM or task scheduler. I was going to get the SCCM to restart the computer if it returns $true but don't know how to so now I'm trying to add a restart within the script itself but don't know how to add it correctly .

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 
}  

回答1:


If (Test-PendingReboot -eq "true") { your code for restart }




回答2:


This sample code should work for you.

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


来源:https://stackoverflow.com/questions/55125957/if-script-runs-true-restart-run-next-part

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!