VMware Cloud Community
VMwareGuy007
Contributor
Contributor
Jump to solution

More PowerShell than PowerCLI, but this corner of the internet has all the answers.

I'm trying to combine certain dates with an array, and I'm having a hard time iterating each item.  Sorry if my question is convoluted, PowerShell is in Blue

The first thing I'm doing is grabbing a date, starting at a specific Day.

$start = Get-date 8-1-2016

What I want to do is output a day over the next year, starting with my $start date, excluding the weekend, which I have below.

($start).AddDays($count)  | where { $_.DayofWeek -ne 'Saturday' -and $_.DayofWeek -ne 'Sunday'  }

Now is the tricky part (at least for me it is)

I have a recurring pattern, and I need the recurring pattern to append each date, the pattern goes for 3 weeks, and then starts over.

So, if my date starts 8-1-2016, here are the dates that I have:

8/1/2016 First set One

8/2/2016 First set

8/3/2016 First set

8/4/2016 First set

8/5/2016 First set Five

8/8/2016 Second set One

8/9/2016 Second set

8/10/2016 Second set

8/11/2016 Second set

8/12/2016 Second set Five

8/15/2016 Third Set One

8/16/2016 Third Set

8/17/2016 Third Set

8/18/2016 Third Set

8/19/2016 Third Set Five

8/22/2016 First Set One

8/23/2016 First Set

8/24/2016 and so on

So, I can get the dates output, but the problem is matching the sets with the date over the next year, 2, years, and on.

Here is what I've tried.

$start = Get-date 8-1-2016

$ArraySet = "First Set One", "First Set Two", "First Set Three", "First Set Four", "First Set five", "Second Set One", "Second Set Two", "Second Set Three", "Second Set Four", "Second Set Five",

                    "Third Set One", "Third Set Two", "Third Set Three", "Third Set four", "Third Set Five"

for ($count = 0; $count -lt 365; $count++) {

    ($start).AddDays($count)  | where { $_.DayofWeek -ne 'Saturday' -and $_.DayofWeek -ne 'Sunday'  } -PipelineVariable String |  foreach {

  

      $string.ToShortDateString()  + ($ArraySet[0..15] | foreach { $_ })

}

}


I'm leaving some of my failed efforts out to keep the post short, I've tried various things, I pretty much understand why it's not working, since every possible item in the array is output, and then the next array is output, but I want one item from each array output, and they should correlate to the dates.


Any help is appreciated, and sorry that this isn't directly PowerCLI.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Not 100% sure I understood the question, but do you mean something like this?

$start = Get-date -Day 1 -Month 8 -Year 2016

$ArraySet = "First Set One", "First Set Two", "First Set Three", "First Set Four", "First Set five",

            "Second Set One", "Second Set Two", "Second Set Three", "Second Set Four", "Second Set Five",

            "Third Set One", "Third Set Two", "Third Set Three", "Third Set four", "Third Set Five"

$undesiredDays = 'Saturday','Sunday'

$i = $j = 0

$day = $start

while($i -le 365){

    $day = $start.AddDays($i)

    while($undesiredDays -contains $day.DayOfWeek){

        $i++

        $day = $start.AddDays($i)

    }

    Write-Host "$($day.ToShortDateString()) $($arraySet[$j%$ArraySet.Count])"

    $i++

    $j++

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Not 100% sure I understood the question, but do you mean something like this?

$start = Get-date -Day 1 -Month 8 -Year 2016

$ArraySet = "First Set One", "First Set Two", "First Set Three", "First Set Four", "First Set five",

            "Second Set One", "Second Set Two", "Second Set Three", "Second Set Four", "Second Set Five",

            "Third Set One", "Third Set Two", "Third Set Three", "Third Set four", "Third Set Five"

$undesiredDays = 'Saturday','Sunday'

$i = $j = 0

$day = $start

while($i -le 365){

    $day = $start.AddDays($i)

    while($undesiredDays -contains $day.DayOfWeek){

        $i++

        $day = $start.AddDays($i)

    }

    Write-Host "$($day.ToShortDateString()) $($arraySet[$j%$ArraySet.Count])"

    $i++

    $j++

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
VMwareGuy007
Contributor
Contributor
Jump to solution

Holy Expletive, that works perfectly.  Other than the fact that I bought your PowerCLI book, there is not any more thanks for me to give.  I'm going to go back and really study what you did, because more than anything, I hope to need less, and less help.  Thank you again. 

0 Kudos
VMwareGuy007
Contributor
Contributor
Jump to solution

One more thanks.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Thanks.

If you have some questions on the code, feel free to ask.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
VMwareGuy007
Contributor
Contributor
Jump to solution

Thanks, I'm going to debug it and look at all the values, and slowly see if I can see what's going on. 

0 Kudos
VMwareGuy007
Contributor
Contributor
Jump to solution

Yeah...umm..help, please.  I've gone through the debugger looking at the values, but I don't understand exactly what's going on.  I think I get the gist of it.  The two while statements are not making sense, I see $i increment in the first one, but then I see it increment again, but $i and $j still have the same count.  Also, the sub expression here:

Write-Host "$($day.ToShortDateString()) $($ArraySet[$j%$ArraySet.Count])" it's more so the last part, the ($ArraySet[$j%$ArraySet.Count]), I don't know what that's doing.

Thanks again for your help.  It's definitely working, but it's difficult to understand. 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, I'll use a screenshot from my editor (it has line numbers).

script.jpg

Loop 1 (line 11-20): main loop, I took 365, but this can be whatever number. This loops over all the days

Loop 2 (line 13-16): when I have a new day (the next one), I skip over all Saturdays and Sundays. That's what the -contains operator is testing.

Line 17: the core of it all. I use the modulo (%) operator to find the entry in the $ArraySet array.

     Whatever number you give, it will return the remainder after division by the number of elements in $ArraySet (0..14), and that is exactly the index into the array I need.

     But since you want them in sequence, I needed to use another counter ($j).

     I can't use $i because we are skipping Saturdays and Sundays. Otherwise we would skip entries in $ArraySet as well. That is why $j is not incremented when we are skipping Sat and Sun.

I hope that makes it bit clearer


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
VMwareGuy007
Contributor
Contributor
Jump to solution

Thank you again, it makes more sense.  I still need to look at it slowly, but things are much clearer now. 

0 Kudos