$BooL = 0
foreach($server in (gc .\ADservers.txt)){
if (Test-Connection $server -Count 1 -Quiet) {
Write-host "$Server is able to connect"
$BooL = 1
break
}
else {
write-host "$Server - Failed"
$BooL = 0
}
}
If ($BooL = 0) {
write-host "None of the Servers mentioned are reachable, The Script will quit..!!"
exit
}
Write-Host "Rest of the Script ... :)"
在上面的代码集中,$ BooL值永远不会更改为1,即使服务器能够ping并且If($ BooL = 0)也没有显示输出或退出脚本的其余部分...任何人都可以帮助我就这个..
答案 0 :(得分:-1)
$BooL = "0"
foreach($server in (gc .\ADservers.txt)){
if (Test-Connection $server -Count 1 -Quiet) {
Write-host "$Server is able to connect"
$BooL = "1"
break
}
else {
write-host "$Server - Failed"
$BooL = "0"
}
}
If ($BooL -eq "0") {
write-host "None of the Servers mentioned are reachable, The Script will quit..!!"
exit
}
Write-Host "Rest of the Script ... :)"
答案 1 :(得分:-1)
$bool
语句中if
的测试需要为if($bool -eq 0)
但是,我不知道你为什么不使用$true
或$false
。我还不清楚您的break
声明的用途 - 您是否希望在任何计算机可以访问后立即继续?在您的其他地方将$bool
重置为0
也没有意义,因为您已经已经脱离了循环,而您之前已将其设置为0
循环。
我建议改为:
$bool = $false
foreach($server in (gc .\ADservers.txt)){
if (Test-Connection $server -Count 1 -Quiet) {
Write-host "$Server is able to connect"
$bool = $true
break
}
else {
write-host "$Server - Failed"
}
}
If (!$bool) {
write-host "None of the Servers mentioned are reachable, The Script will quit..!!"
exit
}
Write-Host "Rest of the Script ... :)"
我还建议使用更好的变量名称。 $bool
非常模糊且无法描述自己 - 最好将其称为$reachable
或其他类似内容。