namdog Posted October 2, 2013 Share Posted October 2, 2013 (edited) I seem to be having an issue and have no idea on how to resolve this. I've only been using AutoIT for about 1 year now. I'm trying to write a program for my company that will remote into 1 of four virtual machines. The criteria should be that if the user is logged in and just closed the RDP session by accident, running this program will log him back into that VM, otherwise it will find the first available VM and connect the user to that. (Similar to VMWare View which costs about $50k for a license) The problem I'm running into is that only one of the "While" loops is working. If I comment one out, the other seems to work fine. Any advice would greatly be appriciated. expandcollapse popup#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** ;Description: Allows the user to connect to the specified VM's. If the user was previously connected to a VM ;and didn't log off, it will reconnect them. $tempfile = "C:\temp\loggedon.log" $nologgedon = "INFO: No tasks are running which match the specified criteria." $username = "User Name: DOMAIN\" ;Change the DOMAIN (PLACE IN ALL CAPS) Dim $line $numberOfHostMachines = 3 Dim $hostMachines[$numberOfHostMachines] ;Place PC names here $hostMachines[0] = "x" $hostMachines[1] = "y" $hostMachines[2] = "z" $currentMachinePointer = 0 $hasFoundPreviousHost = False $hasFoundWorkingHost = False $hasFoundAvailableHost = False While (($currentMachinePointer < $numberOfHostMachines) and Not($hasFoundPreviousHost)) $possibleHost = $hostMachines[$currentMachinePointer] MsgBox(0,"Test1",$possibleHost) If checkPreviousLogin($possibleHost) Then $hasFoundPreviousHost = True $hasFoundWorkingHost = True Else while (($currentMachinePointer < $numberOfHostMachines) and Not($hasFoundWorkingHost)) $possibleHost = $hostMachines[$currentMachinePointer] MsgBox(0,"Test2",$possibleHost) If isComputerAvailable($possibleHost) Then $hasFoundWorkingHost = True Else $currentMachinePointer = $currentMachinePointer + 1 EndIf WEnd $currentMachinePointer = $currentMachinePointer + 1 EndIf WEnd while (($currentMachinePointer < $numberOfHostMachines) and Not($hasFoundWorkingHost)) $possibleHost = $hostMachines[$currentMachinePointer] MsgBox(0,"Test3",$possibleHost) If isComputerAvailable($possibleHost) Then $hasFoundAvailableHost = True Else $currentMachinePointer = $currentMachinePointer + 1 EndIf WEnd If $hasFoundPreviousHost Then connectToHost($hostMachines[$currentMachinePointer]) ElseIf $hasFoundWorkingHost Then connectToHost($hostMachines[$currentMachinePointer]) Else MsgBox(0, "Bad News", "No available machines to connect to.") EndIf Func checkPreviousLogin($hostname) $isAvailable = False RunWait(@COMSPEC & " /c " & 'tasklist /v /fo list /fi "IMAGENAME eq explorer.exe" /s \\' & $hostname & ' > ' & $tempfile) If $tempfile = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf $user = ($username & @UserName) $vmline = FileReadLine($tempfile, 7) If $vmline = $user Then $isAvailable = True EndIf FileClose($tempfile) return $isAvailable EndFunc Func isComputerAvailable($hostName) $isAvailable = False RunWait(@COMSPEC & " /c " & 'tasklist /v /fo list /fi "IMAGENAME eq explorer.exe" /s \\' & $hostname & ' > ' & $tempfile) If $tempfile = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf $line = FileReadLine($tempfile) If @error = -1 Then MsgBox(0, $hostname, $line) If $line = $nologgedon Then $isAvailable = True EndIf FileClose($tempfile) return $isAvailable EndFunc Func connectToHost($hostName) RunWait("C:\Windows\System32\mstsc.exe /v:" & $hostname) EndFunc Edited October 9, 2013 by namdog Link to comment Share on other sites More sharing options...
Solution namdog Posted October 9, 2013 Author Solution Share Posted October 9, 2013 For those who are interested, the problem was with the nested while loop. Created functions to house the while loops. expandcollapse popup#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <GUIConstants.au3> ;Description: Allows the user to connect to the specified VM's. If the user was previously connected to a VM ;and didn't log off, it will reconnect them. $tempfile = "C:\temp\loggedon.log" $nologgedon = "INFO: No tasks are running which match the specified criteria." $username = "User Name: DOMAIN\" ;Change the DOMAIN (PLACE IN ALL CAPS) Dim $line Dim $currentMachinePointer Dim $hasFoundPreviousHost Dim $hasFoundAvailableHost $GUI_Size=100 $GUITitle="VM Launcher" $GUIWording=("Searching." & @CRLF & "Please wait...") GUICreate ($GUITitle, 200, $GUI_Size) $Label = GUICtrlCreateLabel($GUIWording,80,30) $numberOfHostMachines = 3 ; Change to match the number of $hostMachines Dim $hostMachines[$numberOfHostMachines] ;Place PC names here $hostMachines[0] = "x" $hostMachines[1] = "y" $hostMachines[2] = "z" Func findPreviousHost() $currentMachinePointer = 0 $hasFoundPreviousHost = False While (($currentMachinePointer < $numberOfHostMachines) and Not($hasFoundPreviousHost)) GUISetState(@SW_SHOW) ;Show initial installation GUI window Do $possibleHost = $hostMachines[$currentMachinePointer] ;~ MsgBox(0,"Test1",$possibleHost) ;~ MsgBox(0,"PointerTest1",$currentMachinePointer) If checkPreviousLogin($possibleHost) Then $hasFoundPreviousHost = True $hasFoundAvailableHost = True ExitLoop(2) Else $currentMachinePointer = $currentMachinePointer + 1 EndIf Until $currentMachinePointer = $numberOfHostMachines ExitLoop WEnd EndFunc Func findAvailableHost() $currentMachinePointer = 0 $hasFoundAvailableHost = False While (($currentMachinePointer < $numberOfHostMachines) and Not($hasFoundAvailableHost)) Do $possibleHost = $hostMachines[$currentMachinePointer] ;~ MsgBox(0,"Test2",$possibleHost) ;~ MsgBox(0,"PointerTest2",$currentMachinePointer) If isComputerAvailable($possibleHost) Then $hasFoundAvailableHost = True ExitLoop(2) Else $currentMachinePointer = $currentMachinePointer + 1 EndIf Until $currentMachinePointer = $numberOfHostMachines ExitLoop WEnd EndFunc findPreviousHost() If $hasFoundPreviousHost Then connectToHost($hostMachines[$currentMachinePointer]) Else MsgBox(0,"Don't worry","Not currently logged into any systems", 2) EndIf If Not $hasFoundPreviousHost Then findAvailableHost() If $hasFoundAvailableHost Then connectToHost($hostMachines[$currentMachinePointer]) Else MsgBox(0,"Bad News","No available machine to connect to") EndIf EndIf Func checkPreviousLogin($hostName) $isAvailable = False RunWait(@COMSPEC & " /c " & 'tasklist /v /fo list /fi "IMAGENAME eq explorer.exe" /s \\' & $hostName & ' > ' & $tempfile,"",@SW_HIDE) ; Might need to do RunAsWait with credentials If $tempfile = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf $user = ($username & @UserName) $vmline = FileReadLine($tempfile, 7) If $vmline = $user Then $isAvailable = True EndIf FileClose($tempfile) return $isAvailable EndFunc Func isComputerAvailable($hostName) $isAvailable = False RunWait(@COMSPEC & " /c " & 'tasklist /v /fo list /fi "IMAGENAME eq explorer.exe" /s \\' & $hostName & ' > ' & $tempfile,"",@SW_HIDE)) ; Might need to do RunAsWait with credentials If $tempfile = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf $line = FileReadLine($tempfile) If @error = -1 Then MsgBox(0, $hostName, $line) If $line = $nologgedon Then $isAvailable = True EndIf FileClose($tempfile) return $isAvailable EndFunc Func connectToHost($hostName) RunWait("C:\Windows\System32\mstsc.exe /v:" & $hostName) EndFunc Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now