Jump to content

Issue with variable


Recommended Posts

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]

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

  • Moderators

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?

"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

#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]

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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")

 

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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]

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