问题
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