当脚本返回$ 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
}
如果(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