首页 > 解决方案 > 检查变量是否为空,如果为空则重新启动 localhost

问题描述

这是我正在尝试做的相当基本的事情。我有两个 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
}

标签: powershell

解决方案


你的意思是:

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
}

希望这可以帮助!


推荐阅读