我试图让脚本在脚本返回$ 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
}
答案 0 :(得分:-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