如果脚本运行true则重启/运行下一部分

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

当脚本返回$ true时,我正在尝试让脚本重新启动计算机,我打算在SCCM或任务调度程序中运行脚本。我打算让SCCM重新启动计算机,如果它返回$ true但是现在不知道怎么回事我试图在脚本本身添加重启但不知道如何正确添加它。

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 
}  
powershell powershell-v2.0 powershell-v3.0 powershell-v4.0
2个回答
-1
投票

如果(Test-PendingReboot -eq“true”){您的重启代码}


-1
投票

此示例代码应该适合您。

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
© www.soinside.com 2019 - 2024. All rights reserved.