powershell - 检查变量是否为空,如果为空则重新启动 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
}
解决方案
你的意思是:
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
}
希望这可以帮助!
推荐阅读
- java - 在两个字符串之间查找文本(十六进制)
- python - 在 python 中的 Cypher 查询中传递变量
- dart - 如何将 BottomNavigationBar 与 BLoC 一起使用?
- html - ionic 3 storage 获取数据并在 html 中显示
- sql-server - 重叠日期时间的触发器
- spring - 找不到名为“entityManagerFactory”的bean
- android - 如何从其他活动中调用此功能?
- javascript - 使用 Array.includes() 和测试重复项的 JavaScript 问题
- python - np.argsort() 的奇怪/错误输出
- javascript - 我可以在 TypeScript 中只使用 null(而不是 undefined)吗?