Jump to content
Sign in to follow this  
manicjonah

Subscript used on non-accessible variable -- now what?

Recommended Posts

manicjonah

I am very new to AutoIT, just started to work with it today in fact.  With a little help from the Internet I have been working on an AutoIT version of a command line script that I use to lookup user's SID's.  The AutoIT script is working fine with one exception:  If I input invalid data it errors but I can't figure out why.

Opt("MustDeclareVars", 1)
#include <C:\Users\kodiakuser\Desktop\autoit-v3\install\Include\Security.au3>

Global $sUser, $aName

$sUser = InputBox ("U2SID", "Enter the user's UPN or SAM:", "", " M", 350, 180)
If $sUser = "" Then Exit

If @Error <> 0 Then Exit

$aName = _Security__LookupAccountName($sUser)
If @Error <> 0 Then
   MsgBox(0,"U2SID Results", "Invalid UPN or SAM")


Else
   MsgBox(0,"U2SID Results", "User: " & $sUser & @CR &@CR & "SID: " & $aName[0])
EndIf
 

Every time it errors on the last line:   

MsgBox(0,"U2SID Results", "User: " & $sUser & @CR &@CR & "SID: " & $aName[0])

with - "Subscript used on non-accessible variable"

When testing, if I enter a user name that it will find successfully that same line doesn't cause any problems.  What am I missing?

Thanks,

MJ

Share this post


Link to post
Share on other sites
rodent1

The problem is that no data was found, and you don't have an array. It errors out when you try to retrieve data from the array.

add these 2 lines before the else line

elseif Not IsArray($aName) then

   msgbox(0,"","No data found")

Edited by rodent1

Share this post


Link to post
Share on other sites
JLogan3o13

Check that it is an array, first, rather than checking @error:

Opt("MustDeclareVars", 1)
#include <Security.au3>

Global $sUser, $aName

$sUser = InputBox ("U2SID", "Enter the user's UPN or SAM:", "", " M", 350, 180)
If $sUser = "" Then Exit

If @Error <> 0 Then Exit

$aName = _Security__LookupAccountName($sUser)
    If Not IsArray($aName) Then
        MsgBox(0,"U2SID Results", "Invalid UPN or SAM")
    Else
        MsgBox(0,"U2SID Results", "User: " & $sUser & @CR &@CR & "SID: " & $aName[0])
    EndIf
Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
manicjonah

Wow guys, I can't think of a time that I've EVER gotten such a quick reply in a forum.  Thank you very much.

 

How did you know that an array was expected?   Is it because of this in my last line:  $aName[0]?

One more thing, if I wanted to see all of the information gathered by _Security__LookupAccountName($sUser) how would I go about doing that?  

Thanks again for the fast reply!!

MJ

Share this post


Link to post
Share on other sites
JLogan3o13

If you look at the Security.au3 file that you are including (UDF - User Defined Function), you'll see it returns a 3-record array containing the SID, the domain and the snu (SID NAME USE, IIRC, but someone can correct me if I am wrong).

You are, as we all were, on day 1, so there is understandably a lot to learn. However, it will do you a lot of good to review UDFs whenever you include them in your script, so you understand what they are doing, and what types or return values you should expect from the functions you call (functions are the blue, such as _Security__LookupAccountName() ).

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
manicjonah

Thank you for your patience and answers.  I'm sure I'll be seeing you again here in the forums!

MJ

Share this post


Link to post
Share on other sites
JLogan3o13

That's why we're here, welcome to the forum :)


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
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
Sign in to follow this  

×