Powershell 如果脚本运行为真,请重新启动/运行下一部分
我试图让脚本在脚本返回$true时重新启动计算机,我计划在SCCM或任务调度器中运行脚本。 如果SCCM返回$true,我会让它重新启动计算机,但不知道如何重新启动,所以现在我尝试在脚本本身中添加重新启动,但不知道如何正确添加Powershell 如果脚本运行为真,请重新启动/运行下一部分,powershell,powershell-2.0,powershell-3.0,powershell-4.0,Powershell,Powershell 2.0,Powershell 3.0,Powershell 4.0,我试图让脚本在脚本返回$true时重新启动计算机,我计划在SCCM或任务调度器中运行脚本。 如果SCCM返回$true,我会让它重新启动计算机,但不知道如何重新启动,所以现在我尝试在脚本本身中添加重新启动,但不知道如何正确添加 function Test-PendingReboot { if (Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPendin
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
}
如果(测试挂起重新启动-等式“真”)
{
重新启动的代码
}此示例代码应该适合您
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
看起来是这样的
If$true){
应该是这样的If(Test PendingReboot){
[grin]鉴于Test PendingReboot
返回一个布尔值,您不应该将其与字符串进行比较(在这种情况下,它工作时,没有理由使用字符串);直接的解决方法是使用If(Test PendingReboot-eq$true).
;不过,如果(Test PendingReboot).可以。谢谢,但它似乎不起作用,请给我完整的脚本。@TAmoorAHmed它似乎不起作用意味着什么?您收到任何错误消息吗?您是否正确地将括号放在代码中,因此与代码中的if$true不一样)
?我在末尾添加了以下内容,powershell只是挂起:if(Test PendingReboot-eq“true”){Restart Computer-Force}有人能帮忙吗