Jump to content

Variables in and out of While statements


Recommended Posts

Quick question...

Are you not able to have a variable exist both in and outside of a While statement?

The code below is a simple script. I have a While cmd that makes a message box based on the variable $Value. I have the same message box outside of the While statement, but that doesn't work and it loops forever. Is there not a way to pass a result from the the while statement to outside of the while statement?

TIA

#include <Constants.au3>
$ip = "google.com "
$dns = "111.111.111.111"
                $HostCommand = Run(@ComSpec & " /c  nslookup " & $ip & $dns, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
                While 1 
                    $Value = StdoutRead($HostCommand)
                    If @error = -1 Then ExitLoop
                        MsgBox(0,"HostName", $Value)
                Wend
                
MsgBox(0,"HostName", $Value)
exit
Link to comment
Share on other sites

Thanks for the response.

I had tried making a Global variable before posting, guess I should have mentioned that. Using my script as an example:

$ip = "google.com "
$dns = "111.111.111.11"
global $Value
    $HostCommand = Run(@ComSpec & " /c  nslookup " & $ip & $dns, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
                While 1 
                     $Value = StdoutRead($HostCommand)
                    If @error = -1 Then ExitLoop
                        MsgBox(0,"HostName", $Value)
                Wend
                
MsgBox(0,"HostName", $Value)
exit

Your code does work but mine does not. I'm thinking maybe the data for the variable no longer exists outside of the While statement since it's based on a message output. If that's the case is there a way to store that data without having to output it to a file and then read the file?

TIA

Link to comment
Share on other sites

Maybe...

$ip = "google.com "
$dns = "111.111.111.11"
global $Value, $iValue[1]
    $HostCommand = Run(@ComSpec & " /c  nslookup " & $ip & $dns, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
                While 1
                     $Value = StdoutRead($HostCommand)
                    If @error = -1 Then ExitLoop
                    Dim $iValue[UBound($iValue) + 1]
                    $iValue[UBound($iValue)] = $Value
                        MsgBox(0,"HostName", $Value)
                Wend
                
MsgBox(0,"HostName", $Value)


For $x = 1 to UBound($iValue)
    Msgbox(0,'Record:' & $x, $iValue[$x])
Next

... Not Tested

8)

NEWHeader1.png

Link to comment
Share on other sites

Thanks for the response. Your code passes the syntax check but fails execution. Unfortunately your code is beyond my expertise. I think I understand what you're trying to do but I don't know how to fix the error. This is the error I get when executing.

==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

$iValue[uBound($iValue) + 1] = $Value

^ ERROR

Link to comment
Share on other sites

I think Valuater either meant to use $Value instead of $iValue, or else Dim $iValue[uBound($iValue) - 1]

IE Dev ToolbarMSDN: InternetExplorer ObjectMSDN: HTML/DHTML Reference Guide[quote]It is surprising what a man can do when he has to, and how little most men will do when they don't have to. - Walter Linn[/quote]--------------------[font="Franklin Gothic Medium"]Post a reproducer with less than 100 lines of code.[/font]
Link to comment
Share on other sites

#include <Constants.au3>

$ip = "google.com "
$dns = "111.111.111.11"

Dim $Value, $iValue[1]

$HostCommand = Run(@ComSpec & " /c  nslookup " & $ip & " " & $dns, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

While 1
    $line = StdoutRead($HostCommand)
    If @error Then ExitLoop
    MsgBox(0, "STDOUT read:", $line)
Wend

While 1
    $line = StderrRead($HostCommand)
    If @error Then ExitLoop
    MsgBox(0, "STDERR read:", $line)
Wend

Nslookup fails, why are you appending dns to the host name? Also you need a space between $ip and $dns so I added it. Can you post a command that actually works from the command line?

Link to comment
Share on other sites

So I changed the Dim $iValue[uBound($iValue) + 1] to Dim $iValue[uBound($iValue) - 1] I got another error:

==> Array variable subscript badly formatted.:

Dim $iValue[uBound($iValue) - 1]

Dim $iValue[^ ERROR

Then I changed the $iValue to $Value and it gave me the same result of the original code I wrote. I get the 1st msgbox correctly but then second still is blank.

While 1
                     $Value = StdoutRead($HostCommand)
                    If @error = -1 Then ExitLoop
                    Dim $iValue[UBound($Value)  + 1]
                    $iValue[UBound($Value)] = $Value
                        MsgBox(0,"HostName", $Value)
                Wend
Link to comment
Share on other sites

Our DNS server isn't accessible from the outside so I just put something to fill it. I added the DNS server because the DNS server for AD and our main DNS registrations are different and I want to use a particular DNS regardless of what's configured on the machine. For testing purposes the $dns variable can be removed. This code works as well, at least within the while statement.

$ip = "google.com "
global $Value
    $HostCommand = Run(@ComSpec & " /c  nslookup " & $ip, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
                While 1 
                     Global $Value = StdoutRead($HostCommand)
                    If @error = -1 Then ExitLoop
                    MsgBox(0,"HostName", $value)
                        
                Wend
                
MsgBox(0,"HostName", $Value)
exit

I'm trying to keep the script simple to figure out the while statement issue, which is why I'm using message boxes. Basically eventually I want to take the result from the output of the nslookup and compare the machine name and the DNS name to make sure they are the same. I guess I could do it all within the while statement or output results to a file, but I was trying to prune the result from the nslookup outside of the while statement. Maybe it's not possible?

Link to comment
Share on other sites

MsgBox outside of the loop comes up empty because at that point $Value is always empty - last return from StdOutRead (with error).

It has nothing to do with the scope of variables.

So...

$ip = "google.com "
$dns = "111.111.111.11"
$Value = ""
$Total = ""

$HostCommand = Run(@ComSpec & " /c  nslookup " & $ip & $dns, @SystemDir, @SW_HIDE, 6)
While 1
    $Value = StdoutRead($HostCommand)
    If @error Then ExitLoop
    MsgBox(0,"HostName", $Value)
    $Total &= $Value
Wend
                
MsgBox(0,"HostName", $Value)

MsgBox(0,"HostName", $Total)oÝ÷ ØêÕj[µê÷öÊk¢,j"¶ayªëk+ºÚ"µÍÌÍÚH    ][ÝÙÛÛÙÛKÛÛH    ][ÝÂÌÍÙÈH ][ÝÌLLKLLKLLKLI][ÝÂÛØ[    ÌÍÕ[YK   ÌÍÚU[YVÌWBÌÍÒÜÝÛÛ[X[H[ÛÛTÜXÈ [È ][ÝÈØÈÛÛÚÝ  ][ÝÈ  [È ÌÍÚ  [È ÌÍÙËÞÝ[QÕ×ÒQK
BÚ[HB  ÌÍÕ[YHHÝÝ]XY
    ÌÍÒÜÝÛÛ[X[
BYÜ[^]ÛÜQ[H  ÌÍÚU[YVÕPÝ[
    ÌÍÚU[YJJÌWB ÌÍÚU[YVÕPÝ[
    ÌÍÚU[YJKLWHH ÌÍÕ[YBÙÐÞ
    ][ÝÒÜÝ[YI][ÝË ÌÍÕ[YJBÙ[ÙÐÞ
    ][ÝÒÜÝ[YI][ÝË ÌÍÕ[YJBÜ    ÌÍÞHHÈPÝ[
    ÌÍÚU[YJKLBÙØÞ
    ÌÎNÔXÛÜÌÎNÈ [È ÌÍÞ  ÌÍÚU[YVÉÌÍÞJB^
Edited by Siao

"be smart, drink your wine"

Link to comment
Share on other sites

GUI Version - just for fun

#include <GUIConstants.au3>
#include <GuiEdit.au3>

GUICreate("nslookup gui")  ; will create a dialog box that when displayed is centered
GUISetState (@SW_SHOW)       ; will display an empty dialog box

;Create log area
$LOG = GUICtrlCreateEdit("", 10, 10, 380, 250, $ES_AUTOVSCROLL+$WS_VSCROLL+$ES_READONLY)
GUICtrlSetBkColor(-1,0xFFFFFF)
GUICtrlSetFont($LOG, 8, -1, -1)

;Hostname input
GUICtrlCreateLabel ("Hostname:",  30, 285, 50)
$INPUT1 = GUICtrlCreateInput ( "google.com",90, 280, 230, 20)

;Dns input
GUICtrlCreateLabel ("DNS:",  30, 315, 50)
$INPUT2 = GUICtrlCreateInput ( "111.111.111.11",90, 310, 230, 20)

;Go button
$GO = GUICtrlCreateButton ("GO",  150, 350, 100)

;Update log text area with new line
Func UPDATELOG($M, $MODE)
    Dim $DASHES
    
    Switch $MODE
        ;End line with carriage return
        Case 1
            GUICtrlSetData ($LOG, $M & @CRLF, 1)
        ;End line without carriage return
        Case 2
            GUICtrlSetData ($LOG, $M, 1)
        ;Wrap line with dashes
        Case 3
            For $X = 0 to StringLen ($M)
                $DASHES = $DASHES & Chr (150)
            Next
            
            GUICtrlSetData ($LOG, $DASHES & @CRLF & $M & @CRLF & $DASHES & @CRLF, 1)
    EndSwitch
    _GUICtrlEditScroll($LOG, $SB_SCROLLCARET)
EndFunc

Func NSLOOKUP()
    $ip = GuiCtrlRead($INPUT1)
    $dns = GuiCtrlRead($INPUT2)
    global $Value, $iValue[1]

    $HostCommand = Run(@ComSpec & " /c  nslookup " & $ip, @SystemDir, @SW_HIDE, 6)
    While 1
        $Value = StdoutRead($HostCommand)
        If @error Then ExitLoop
        ReDim $iValue[UBound($iValue)+1]
                    
        UPDATELOG($Value, 1)
                    
        $iValue[UBound($iValue)-1] = $Value
    Wend
EndFunc

; Run the GUI until the dialog is closed
While 1
    Switch GUIGetMsg()
        Case $GO ;GO button clicked
            NSLOOKUP()
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
Wend
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...