Jump to content

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


Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • Moderators

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Moderators

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

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...