Using an array in Powershell and trying to update it from multiple functions

Edward Spencer 1 Reputation point
2022-09-13T19:05:02.503+00:00

I have an array that is declared at the parent level.

I want to be able to use this array in multiple functions and I want to be able to update that array within those functions as needed (not adding to it, but modifying the values within the array).

I have tried declaring it as Global, but that does not seem to allow me to modify the data in the array.

Any help you can provide would be greatly appreciated.

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-09-13T19:48:15.723+00:00

    Well, without seeing what those function do (i.e., you posting the code you've written) it's hard to say.

    It's certainly possible to modify the contents of an array:

    $a = 1,2,3  
    function funca{  
        $a[1]=20  
    }  
    function funcb{  
        $a[2]=30  
    }  
    function funcc{  
        $a[0] = "Look, Ma! No hands!"  
    }  
    "--- unmodified array ---"  
    $a  
    funca  
    funcb  
    funcc  
    "--- modified array ---"  
    $a  
    
    0 comments No comments

  2. Michael Taylor 60,161 Reputation points
    2022-09-13T19:50:03.277+00:00

    Arrays are annoying to use in PS. If you're not adding items to them then they should be fine. Here's a simplistic example.

       $items = @(1, 2, 3, 4)  
         
       function IncrementArray ( $arr )  
       {  
          for ($index = 0; $index -t $arr.length; ++$index) {  
             $arr[$index] *= 2  
          }  
       }  
         
       IncrementArray($items)  
    

    Each time the function is called the elements in the array are doubled. To identify the array to modify you should pass it as a parameter to the function. This is the preferred approach.

    If for some reason you don't want to pass the array to your functions then you should really reconsider your approach. But PS will find variables defined outside the function if the names aren't overridden inside the function. But this sets up functions that are not as reusable as you might hope.


  3. Edward Spencer 1 Reputation point
    2022-09-13T20:20:23.003+00:00
    $DBArray = @(  
                ("DBName","Sever", "Instance","DBPath","R01","R02","R03","R04","R05",$False,$False,$False,$False,$Fasle),  
                ("DB01","DEV-SRV01", "INSTDV01","BIN01","BR01","BR02","BR03","BR04","BR05",$False,$False,$False,$False,$Fasle),  
                ("DB02","DEV-SRV02", "INSTDV02","BIN02","CR01","CR02","CR03","CR04","CR05",$False,$False,$False,$False,$Fasle),  
                ("DB03","DEV-SRV03", "INSTDV03","BIN03","DR01","DR02","DR03","DR04","DR05",$False,$False,$False,$False,$Fasle))  
      
    $FetchButton.Add_Click({ GoFetchRX $DBArray })  
      
    function GoFetchRX {  
        Param ( $DBArray )  
      
        for ( $index = 0; $index -lt $DBArray.count; $index++)  
        {  
            Switch ($index)  
            {  
                0 {  
                    #Do Nothing, This is the header  
                }  
                  
                1 {  
                        if ($DBArray[$index][4] -ne "N/A")  
                        {  
                               $IsValCheck = CHECKRVAL $DBArray[$index][4]  
                               if(IsValCheck)  
                               {     $DBArray[$index][9]  = $True   
                               } else  
                               {     $DBArray[$index][9]  = $false  
                               }  
                        }  
                  2 { .  
                       .  
                       .  
                      }  
              }  
    }  
                                   
    

    There is a lot of code, So I tried to give something as close as I can.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.