Powershell嵌套vs try / catch

时间:2016-06-30 21:12:08

标签: loops powershell if-statement

我认为我会以正确的方式解决这个问题,但它似乎无法发挥作用。我试图嵌套if / elseif /然后根据响应循环一些命令。在try / catch设置中这会更正确吗?

这是代码..(希望我能做到这一点!)

$ecuPort = new-Object System.IO.Ports.SerialPort COM8,9600,None,8,one
$ecuPort.Open()

# Set OBD Mode/PID
$obdMode = "27"                                      # Mode
$obdPid = "02"                                       # PID  change value to 1 to get the seed 
# ----------------------
[int]$i = '1'                                        #  Counter Start

:keyLoop while ( $i -lt '65535') {  

$key  = "{0:X}" -f [int]$i                           # ---- Loop setup----
$request = $obdMode + $obdPid + $key.PadLeft(4,"0")
$ecuPort.Write($request)                             # ---Send Request---
start-sleep -m 400
$input = $ecuPort.ReadExisting() # -split " "          # ---Get Response----  
$response = $input[2,3] -join ""

}
 if ($response -eq '35') {                           # ----Main Logic-----

  $key | Add-Content KeyLogger.txt                  # Log keys for testing
  $i++                                              # advance key counter
  Start-Sleep -m 11000                              # 10 second delay
  continue keyLoop 
 }
  elseif ($response -eq '34') {

   Write-Host("Unlock Key:") $key
   $key | Out-File Unlocked.txt
   # blink led
   $ecuPort.Close()
   break  
}  
 elseif ($response -eq '36') {

   $arduPort.Open()
   Start-Sleep -m 400
   $arduPort.Write("off")
   Start-Sleep -m 5000
   $arduPort.Write("on")
   $arduPort.Close()
   continue keyLoop
  }   
   elseif ($response -eq '37') {
   Start-Sleep -Milliseconds 11000
   continue keyLoop
  }
   then {

      # 

        }

我还在学习,所以对我很轻松,我意识到这是非常基本的东西,但我仍然在语法上很弱。我试图为每个if运行脚本块,然后继续开始':keyLoop标签'循环。因此,继续声明。似乎永远不会在循环中运行代码! 感谢

0 个答案:

没有答案