0

I've tried searching for this problem but haven't found anything helpful.

I'm trying to get a Powershell Function to loop over an array of strings. My twist is I'm trying to add a second array. The first array has the days of the week. The second array is a timestamp for example 2015-01-19

What I have working so far is below. I'm aware that using Get-Date then formatting that to what works (by adding and formatting the results) might be easier, I'm open to suggestions. -Thanks, I really do appreciate the assistance!

Function My-Test{
    param(
        [string[]]$arr,
        [string[]]$arr2
    )
    Foreach($day in $arr){
        "$day is the best day to read something."
    }
    Foreach($var2 in $arr2){
        echo $arr2
    }
}

$days = @("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
$var2 = @("1","2","3","4","5","6","7")

My-Test -arr $days -arr2 $var2

Test below

$array = @(
 ("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"),
 ("2015-01-01","2015-01-02","2015-01-03","2015-01-04","2015-01-05","2015-01-06","2015-01-07")
 )
$array[0][2]

The $array does not return the two properties, it returns Wednesday

8
  • You should simplify the code to isolate the question you're asking. The problem you've stated has nothing to do with IE. Commented Jan 20, 2015 at 4:55
  • Also, show what you've tried with the second array parameter. Commented Jan 20, 2015 at 4:55
  • No, you are correct IE has nothing to do with this question however it's part of my script. Question has been edited. Commented Jan 20, 2015 at 5:27
  • The code you posted doesn't error, and it gives reasonable output. What output were you expecting? Commented Jan 20, 2015 at 5:37
  • 1
    In your second Foreach, you should echo $var2, you are echoing the entire array. Commented Jan 20, 2015 at 5:54

1 Answer 1

1

Answering the followup question.

You could return the two values (I assume $arr and $arr2) in either a single array or in an array containing two arrays:

Function My-Test{
    param(
        [string[]]$arr,
        [string[]]$arr2 
    )
    Foreach($day in $arr){
       write-host "$day is the best day to read something."   
    }
    Foreach($var2 in $arr2){
        write-host $var2
    }

    $ret = $arr+$arr2
    return $ret
}

$days = @("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
$var2 = @("1","2","3","4","5","6","7")

$ret = My-Test -arr $days -arr2 $var2

Then $ret.Count is 14 and $ret contains the combined $arr + $arr2

Other possibility is to create an array of arrays, or a two dimentional array:

$ret = @()
$ret += ,$arr
$ret += ,$arr2
return $ret

In this case $ret[0] contains the weekdays, and $ret[0][0] is Monday, $ret[0][1] is Tuesday, etc. and $ret[1] contains the second array.

PS. You don't need to write return you could simply write $ret as it's implicit that you are returning that value, but it's simpler to explain with that example in my opinion. In fact you don't even need to create a $ret variable, you could just write

@($arr,$arr2)

EDIT

You can only access elements in an array one at a time.

Lets look at your new multidimentional array

$array = @(
 ("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"),
 ("2015-01-01","2015-01-02","2015-01-03","2015-01-04","2015-01-05","2015-01-06","2015-01-07")
 )

Let's see what $array[0] contains:

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

So $array[0][0] is the first element of this array, Monday, $array[0][1] is the second, Tuesday, etc.

If we look at the second array:

$array[1]

It contains:

2015-01-01
2015-01-02
2015-01-03
2015-01-04
2015-01-05
2015-01-06
2015-01-07

The first element for the second array is $array[1][0], 2015-01-01, the second is $array[1][1], 2015-01-02, etc.

Sign up to request clarification or add additional context in comments.

5 Comments

I tried the multidimensional array approach but this quickly went over my head. Edited question with multidimensional array code.
And that means I could do this ($array[0][4] + " " + $array[1][4]) to get the output Friday 2015-01-05
OK I've got stuck adding this to an existing script. This script takes a T-SQL query and outputs the results as an Excel spreadsheet. I'm trying to run 7 queries and create 1 spreadsheet containing 7 worksheets. I just worked out the code to run the 7 queries but I get 7 spreadsheets. I've got the code to add a new worksheet to an existing workbook but can't figure out how to pass two arrays on two parameters to this function.
I would recommend writing a new question, and provide an up to date version of your script, and the desired results.
Things are working as is however I'm running into the Zombie Excel proc that won't close. If you open the spreadsheet then close it, it'll close but I want the script to do so (Powershell!!! You clean up after yourself!) but the code is far from optimized. But I'm curious to see what to community thinks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.