检查变量是否为空,如果变量为空,则重新启动本地主机

时间:2019-03-08 17:40:28

标签: powershell

这是我要尝试的相当基本的事情。我有两个填充以下变量的SQL查询。我希望查询运行直到它们为空,然后重新启动本地主机。

Do {
 database Query that populates two variables. If both variables are empty then reboot the local host. 

}
until($jobquery -and $testQuery)

{
 restart-computer -computername "localhost" -force
}

1 个答案:

答案 0 :(得分:1)

您的意思是:

until(!$jobquery -and !$testQuery)

或者一个很好的建议是添加try catch错误处理,这样它将看起来像这样:

Do{
    try{
       database Query that populates two variables. If both variables are empty then reboot the local host. 
    }catch{
        if(!$jobquery){
            Write-Host "Unable to do query because jobquery is empty. Error: $($_.Exception.Message)" -ForegroundColor Red
        }elseif(!$testQuery){
            Write-Host "Unable to do query because testquery is empty. Error: $($_.Exception.Message)" -ForegroundColor Red
        }else{
            Write-Host "Unable to do query because the following error: $($_.Exception.Message)" -ForegroundColor Red
        }
    }
}until(!$jobquery -and !$testQuery)

{
 restart-computer -computername "localhost" -force
}

希望这会有所帮助!