none
Index was outside bounds of array error RRS feed

  • Question

  • So I have the below code and I'm trying to enter values into an array using input from the user. Whenever I run the script, it goes through it all, but spits out an error before the "Write-Host" line saying the index was outside bounds of the array. I even output $i and it shows that it increments properly, so I'm not sure what it's trying to say. Powershell is telling me the error is at the line "$LogName[$i] = Read-Host -Prompt "Enter the name of Log #$j"

     
    $LogName = @() #Declaration of an empty array 
    $NumberOfLogs = Read-Host -Prompt "How many logs do you want to retrieve?"

    for ($i=0
    $i
    -lt $NumberOfLogs
    $i++) 
    { 
    $j = $i+1
    
    $LogName [$i] = Read-Host -Prompt "Enter name of Log #$j"
    
    Write-Host $LogName[$i]
    }



    Wednesday, February 17, 2016 8:09 PM

Answers

  • This is what ended up working for me. It seems like many other powershell sites make arrays look like what I posted originally.

    $NumberOfLogs = Read-Host -Prompt "How many logs do you want to retrieve (If you want to retrieve the Application and System event log, enter '2')"
    $LogName = New-Object string[] $NumberOfLogs #Declaration of an empty array
    
    for ($i=0
    $i -lt $NumberOfLogs
    $i++) 
    { 
    $j = $i+1
    $LogName[$i] += Read-Host -Prompt "Enter name of Log #$j"
    Write-Host $LogName[$i]
    }

    • Marked as answer by Joseph Larrew Wednesday, February 17, 2016 9:08 PM
    Wednesday, February 17, 2016 8:57 PM

All replies

  • You must have posted very incomplete code. There is no declaration for the $NumberOfLogs variable.

    Also, $j is not initialized. (It's getting incremented by a side-effect of the fact that PowerShell creates the variable for you and initializes it as $null, which when evaluated as an Int, is zero.)


    -- Bill Stewart [Bill_Stewart]

    Wednesday, February 17, 2016 8:22 PM
    Moderator
  • Yeah, the code is not complete, but $NumberOfLogs gets declared and gets a value from a Read-Host cmdlet. I'll modify the original question. The only thing I use $j for is user readability and it works. When I run the script, it asks "Enter name of Log#1" the first time it comes up.
    Wednesday, February 17, 2016 8:26 PM
  • Consider the output of this very short tester:

    $LogName = @()
    $LogName[0] = 'a'


    Wednesday, February 17, 2016 8:30 PM
  • Gives me the same "index was out of bounds" error.
    Wednesday, February 17, 2016 8:33 PM
  • Exactly. What were you expecting to happen?


    -- Bill Stewart [Bill_Stewart]

    Wednesday, February 17, 2016 8:34 PM
    Moderator
  • Mike's suggestion gave me the same error I've been experiencing. I've been thinking that the line  "$LogName[$i] = Read-Host -Prompt "Enter the name of Log #$j" would put a value into the array. Why doesn't it?
    Wednesday, February 17, 2016 8:40 PM
  • Arrays don't work that way.

    You can add an element to an array like this:


    $LogName += "This is a new element"
    

    This is pretty inefficient, though. Appending lots of elements to an array sometimes suggests a bad design.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, February 17, 2016 8:46 PM
    Moderator
  • This is an alternate you can use:

    $LogName = New-Object -TypeName System.Collections.ArrayList
    $LogName.Add('Log #1') | Out-Null
    $LogName.Add('Log #2') | Out-Null
    $LogName.Add('Log #3') | Out-Null
    
    $LogName


    Wednesday, February 17, 2016 8:50 PM
  • This is what ended up working for me. It seems like many other powershell sites make arrays look like what I posted originally.

    $NumberOfLogs = Read-Host -Prompt "How many logs do you want to retrieve (If you want to retrieve the Application and System event log, enter '2')"
    $LogName = New-Object string[] $NumberOfLogs #Declaration of an empty array
    
    for ($i=0
    $i -lt $NumberOfLogs
    $i++) 
    { 
    $j = $i+1
    $LogName[$i] += Read-Host -Prompt "Enter name of Log #$j"
    Write-Host $LogName[$i]
    }

    • Marked as answer by Joseph Larrew Wednesday, February 17, 2016 9:08 PM
    Wednesday, February 17, 2016 8:57 PM
  • This is what ended up working for me. It seems like many other powershell sites make arrays look like what I posted originally.

    That's because it is:

    PS C:\> $LogName = @()
    
    PS C:\> $LogName.GetType()
    
    IsPublic IsSerial Name     BaseType    
    -------- -------- ----     --------    
    True     True     Object[] System.Array


    Wednesday, February 17, 2016 9:03 PM
  • I've seen some sites that declare an array the way you just did and then would go on to say "Add an element to your array using the += operator." So I tried that as well, but it didn't work either. Apparently it would have only worked if I hadn't declared the array beforehand. I didn't really see much on arrays and for loops together, but it could just be because I don't know how to Google or something, I don't know.
    Wednesday, February 17, 2016 9:08 PM