Sign in to follow this  
Followers 0
shornw

Issue with variable

21 posts in this topic

#1 ·  Posted

Hi all,

This is quite frustrating (probably user error) but hopefully someone will be able to see and explain what I'm missing / doing wrong.

Basically created an array (which is working fine) then trying to use an element of the array as a SWITCH but get 'Array variable has incorrect number of subscripts or subscript dimension range exceeded'.  The variable get's populated correctly in the MsgBox  but fails even when I tried declaring a new variable ie $os = $aOS[1][1]

$aOS = _AD_GetObjectProperties( $Comp[$i], "OperatingSystem")
;   MsgBox(0, "", $aOS[1][1])
    Switch $aOS[1][1]

Any ideas gratefully received and thanks in advance

 

 

 


[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Share this post


Link to post
Share on other sites



#2 ·  Posted

Have you tried checking the value of @error after the call to the AD function?

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

No, because an _ArrayDisplay shows the array populated correctly.  Also, the variable gets populated correctly in the MsgBox I added to test it

Edited by shornw

[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Share this post


Link to post
Share on other sites

#4 ·  Posted

If that were true in all instances, then you wouldn't be running into that error message. ;-)

Share this post


Link to post
Share on other sites

#5 ·  Posted

3 minutes ago, shornw said:

No, because an _ArrayDisplay shows the array populated correctly.  Also, the variable gets populated correctly in the MsgBox I added to test it

@shornw

Which of the two arrays are you displaying? :)


Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites

#6 ·  Posted

How about providing your whole code, rather than just a couple lines, or at least a reproducer, so we can see (and replicate) what you're doing?

1 person likes this

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

Share this post


Link to post
Share on other sites

#7 ·  Posted

#include<AD.au3>
#include<Array.au3>


_AD_Open()


 $Comp = _AD_GetObjectsInOU("OU=?????, DC=?????, DC=??????, DC=???", "(objectclass=computer)")
 _ArrayDisplay($comp)
 Dim $aNoOS[1][2]
 Dim $aW2k3[1][2]

For $i = 1 To UBound($Comp) -1
    $aOS = _AD_GetObjectProperties( $Comp[$i], "OperatingSystem")
    Switch $aOS[1][1]
        Case StringInStr($aOS, "MAC")
            ContinueLoop
        Case ""
            _ArrayAdd($aNoOS, $Comp[$i] & "|" & $aOS[1][1])
            MsgBox(0, "No OS", $comp[$i] & @TAB & $aOS)
        Case StringInStr($aOS, "Windows Server 2003")
            _ArrayAdd($aw2k3, $comp[$i] & "|" & $aOS[1][1])
    EndSwitch
Next

 


[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Share this post


Link to post
Share on other sites

#8 ·  Posted

12 minutes ago, FrancescoDiMuro said:

Which of the two arrays are you displaying? :)

For test purposes I always do an _ArrayDisplay to confirm expected results so I have displayed both arrays and the results were as expected


[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

25 minutes ago, shornw said:

Case StringInStr($aOS, "Windows Server 2003")

Maybe here?

$aOS becomes an array at the starting of the loop, and everytime the loop repeats...

So, you should check with the right syntax:

Case StringInStr($aOS[1][1], "Windows Server 2003") ; Maybe... ?

 

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites

#10 ·  Posted

Thank you for this Franceso - you are right, that would have failed and would have come to light later, however what I'm trying to identify is why the following line causes the script to fail with the error ' Array variable has incorrect number of subscripts or subscript dimension range exceeded '

Switch $aOS[1][1]

Incidentally, it works fine if I use If - Then instead of Switch - Case

For $i = 1 To UBound($Comp) -1
    $aOS = _AD_GetObjectProperties( $Comp[$i], "OperatingSystem")
    If UBound($aOS) < 2 Then
        _ArrayAdd($aNoOS, $comp[$i] & "| No OS Info")
        ContinueLoop
    ElseIf $aOS[1][1] = "Windows Server 2003" Then
        _ArrayAdd($aw2k3, $comp[$i] & "|" & $aOS[1][1])
    ElseIf $aOS[1][1] = "Windows Server 2008 R2 Standard" Then
        _ArrayAdd($aw2k8, $comp[$i] & "|" & $aOS[1][1])
    EndIf

Next

_ArrayDisplay($aNoOS, "No OS")
_ArrayDisplay($aw2k3, "W2K3")
_ArrayDisplay($aw2k8, "W2K8")

 


[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Share this post


Link to post
Share on other sites

#11 ·  Posted

@shornw

Happy to have helped :)

What type of value are you trying to compare? String? Integer? :)


Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites

#12 ·  Posted

the value is a string - it's the Operating System of devices extracted from AD

What you pointed out would have caused a failure to populate an array later but l don't understand what is causing the failure when using the variable with Switch


[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Share this post


Link to post
Share on other sites

#13 ·  Posted

49 minutes ago, shornw said:

Incidentally, it works fine if I use If - Then instead of Switch - Case

Does it work with the Switch - Case scenario if you add in the UBound check before the Switch?

Share this post


Link to post
Share on other sites

#14 ·  Posted

@shornw

Good morning :)

Can you try to replace the Switch with the Select statement? :)


Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites

#15 ·  Posted

16 hours ago, Danp2 said:

Does it work with the Switch - Case scenario if you add in the UBound check before the Switch?

No - With Ubound check/continueloop, it runs but operates the ContinueLoop statement so ignores anything beyond it and doesn't populate any of the arrays. I Changed the behaviour of the  If Ubound and it failed.  Iadded a new MsgBox to identify the value of Ubound before it hits the Switch command - as expected the value is generally 2

For $i = 1 To UBound($Comp) -1

    $aOS = _AD_GetObjectProperties( $Comp[$i], "OperatingSystem")
    MsgBox(0, "Ubound", UBound($aOS))       ; added to test whether the variable is populated
    # If UBound($aOS) < 2 Then ContinueLoop
    If UBound($aOS) < 2 Then _ArrayAdd($aNoOS, $Comp[$i] & "|" & $aOS[1][1])    ; added to test if the script runs
Switch $aOS[1][1]
        Case StringInStr($aOS, "MAC")
            ContinueLoop
        Case ""
            _ArrayAdd($aNoOS, $Comp[$i] & "|" & $aOS[1][1])
            MsgBox(0, "No OS", $comp[$i] & @TAB & $aOS)
        Case StringInStr($aOS, "Windows Server 2003")
            _ArrayAdd($aw2k3, $comp[$i] & "|" & $aOS[1][1])
        Case StringInStr($aOS, "Windows Server 2008 standard") Or StringInStr($aOS, "Windows 2008 Enterprise")
            _ArrayAdd($aw2k8, $comp[$i] & "|" & $aOS[1][1])
        Case StringInStr($aOS, "Windows Server 2008 R2")
            _ArrayAdd($aw2k8R2, $comp[$i] & "|" & $aOS[1][1])
        Case StringInStr($aOS, "Windows Server 2012 Standad")
            _ArrayAdd($aW2k12, $comp[$i] & "|" & $aOS[1][1])
        Case StringInStr($aOS, "Windows Server 2012 R2")
            _ArrayAdd($aW2k12R2, $comp[$i] & "|" & $aOS[1][1])
        Case StringInStr($aOS, "Windows Server 2016")
            _ArrayAdd($aW2k16, $comp[$i] & "|" & $aOS[1][1])
    EndSwitch

    If UBound($aOS) < 2 Then
        _ArrayAdd($OperatingSys, $Comp[$i] & "|" & "No OS information")
        ContinueLoop
    Else
    _ArrayAdd($OperatingSys, $Comp[$i] & "|" & $aOS[1][1])
    EndIf




Next

_ArrayDisplay($aw2k3, "W2K3")
_ArrayDisplay($aw2k8, "W2K8")

 


[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Share this post


Link to post
Share on other sites

#16 ·  Posted

I meant something like this --

For $i = 1 To UBound($Comp) -1

    $aOS = _AD_GetObjectProperties( $Comp[$i], "OperatingSystem")
    
    If UBound($aOS) < 2 Then
        _ArrayAdd($OperatingSys, $Comp[$i] & "|" & "No OS information")
        ContinueLoop
    EndIf
    
    Select
        Case StringInStr($aOS[1][1], "Windows Server 2003")
            _ArrayAdd($aw2k3, $comp[$i] & "|" & $aOS[1][1])
        Case StringInStr($aOS[1][1], "Windows Server 2008 standard") Or StringInStr($aOS[1][1], "Windows 2008 Enterprise")
            _ArrayAdd($aw2k8, $comp[$i] & "|" & $aOS[1][1])
        Case StringInStr($aOS[1][1], "Windows Server 2008 R2")
            _ArrayAdd($aw2k8R2, $comp[$i] & "|" & $aOS[1][1])
        Case StringInStr($aOS[1][1], "Windows Server 2012 Standad")
            _ArrayAdd($aW2k12, $comp[$i] & "|" & $aOS[1][1])
        Case StringInStr($aOS[1][1], "Windows Server 2012 R2")
            _ArrayAdd($aW2k12R2, $comp[$i] & "|" & $aOS[1][1])
        Case StringInStr($aOS[1][1], "Windows Server 2016")
            _ArrayAdd($aW2k16, $comp[$i] & "|" & $aOS[1][1])
    EndSelect
Next

_ArrayDisplay($aw2k3, "W2K3")
_ArrayDisplay($aw2k8, "W2K8")

 

Share this post


Link to post
Share on other sites

#17 ·  Posted

Thanks for this Danp2 - I ran this and on the plus side it didn't bomb out.  On the downside, the $aw2k8 array failed to populate which is of no consequence as I'm sure can easily be remediated.  I know this works perfectly with If - Then which is little difference to Select - Case so I'm able to produce a working script.

What's driving my curiosity now is why Switch fails to work with variable $aOS[1][1] - it must be something to do with the output as $test = $aOS[1][1] also fails yet a MsgBox(0, "Test", $aOS[1][1]) gets populated fine in the same location which is messing with my head.  I need to understand cos I don't like unexpected :'(


[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Share this post


Link to post
Share on other sites

#18 ·  Posted

21 hours ago, Danp2 said:

Have you tried checking the value of @error after the call to the AD function?

I asked this earlier... and it still applies here. Basically, you need to confirm the results of the function call before moving on. Check @error, test using IsArray(), do the UBound check, etc. before assuming that the array contains the data you are expecting.

Share this post


Link to post
Share on other sites

#19 ·  Posted

OK... so the below confirms an array but goes into a loop at the MsgBox and doesn't process Switch onwards

For $i = 1 To UBound($Comp) -1
    $aOS = _AD_GetObjectProperties( $Comp[$i], "OperatingSystem")
    If IsArray($aOS) Then MsgBox(0, "ArrayTest", "Array confirmed")

Switch $aOS[1][1]

whereas the below fails immediately it hits the Switch

For $i = 1 To UBound($Comp) -1
    $aOS = _AD_GetObjectProperties( $Comp[$i], "OperatingSystem")
    If Not IsArray($aOS) Then MsgBox(0, "ArrayFail", "No Array")

Switch $aOS[1][1]

@error of the _AD_GetObjectProperties returns 0

If the array DOESN'T contain the data I'm expecting to see, how is it populating  MsgBox(0, "Test", $aOS[1][1])  and how is it working correctly with If - Then or Select - Case?


[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Share this post


Link to post
Share on other sites

#20 ·  Posted

Just wondering if anyone can offer any suggestion as to why Switch seems to fail with multidimensional array field or if anyone has used this combination successfully....

Any suggestions or ideas gratefully received


[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

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  
Followers 0