?
Solved

PowerShell Code - Is a date in this Array/HashTable

Posted on 2015-02-24
6
Medium Priority
?
166 Views
Last Modified: 2015-02-24
Okay, I'm still learning, and I am trying to write a function (which will basically be recursive) to return a 'good' date. I want to build either an array or hash table (not sure which) of holidays. I do not have any reason to retrieve a specific holiday, I only want to know if a specific date is in the table.
Basically, I pass a date to the function. Using Switch() if it is a Saturday, Sunday or Monday, I subtract either 1, 2 or 3 days respectively. Any other date, I subtract 1 day. Then I want to check if the (adjusted) date is in the array/hash table. If it is, I want to call the function again, passing date - 1. The return value will be a good date.

Thus, if I am working with a Tuesday and the previous day is a holiday, the call to the function would calculate the Monday, determine it is a holiday, and call the function passing Sunday's date (calculated Monday - 1), which would in turn return Friday's date.

Here is what I have so far, but I think, from what I'm reading, what I really need is a hash table. Just having a bit of trouble putting it together. Note that the value passed (and needed to be returned) for $procDate is a string in the format of yyyy-mm-dd. As I indicated, I do not need to know what the date is, associate it with any specific holiday, etc. I just need to know if the date I am working with is in the array/table.
$holidays = @("2013-11-28","2013-12-25","2014-01-01","2014-01-20","2014-02-17","2014-04-18","2014-05-26","2014-07-04","2014-09-01","2014-02-17","2014-11-27","2014-12-25","2014-01-01","2014-01-19")

Function GetWorkDay($procDate)
{
	$dow = (Get-Date($procDate)).DayOfWeek
	$procDate = Switch ($dow)
	{
		"Saturday" { (Get-Date($processDate)).AddDays(-1).ToString("yyyy-MM-dd") }
		"Sunday" { (Get-Date($processDate)).AddDays(-2).ToString("yyyy-MM-dd") }
		"Monday" { (Get-Date($processDate)).AddDays(-3).ToString("yyyy-MM-dd") }
		default { (Get-Date($processDate)).AddDays(-1).ToString("yyyy-MM-dd") }
	}
	If ($procDate is a holiday) # pseudo-code
	{
		$procDate = GetWorkDay((Get-Date($procDate)).AddDays(-1))
	}
	Return $procDate
}

Open in new window

0
Comment
Question by:dbbishop
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 40629334
Hi, first off, an array would be more suitable for single values.  Second, in your Switch you had $processDate and not $procDate.

Then, to determine whether the date exists, we use
$holidays -contains $procDate

Regards,

Rob.

$holidays = "2013-11-28","2013-12-25","2014-01-01","2014-01-20","2014-02-17","2014-04-18","2014-05-26","2014-07-04","2014-09-01","2014-02-17","2014-11-27","2014-12-25","2014-01-01","2014-01-19"

Function GetWorkDay($procDate)
{
	$dow = (Get-Date($procDate)).DayOfWeek
	$procDate = Switch ($dow)
	{
		"Saturday" { (Get-Date($procDate)).AddDays(-1).ToString("yyyy-MM-dd") }
		"Sunday" { (Get-Date($procDate)).AddDays(-2).ToString("yyyy-MM-dd") }
		"Monday" { (Get-Date($procDate)).AddDays(-3).ToString("yyyy-MM-dd") }
		default { (Get-Date($procDate)).AddDays(-1).ToString("yyyy-MM-dd") }
	}
    If ($holidays -contains $procDate)
	{
        $procDate = GetWorkDay((Get-Date($procDate)).AddDays(-1))
	}
	Return $procDate
}

Open in new window

0
 
LVL 46

Expert Comment

by:aikimark
ID: 40629386
You can avoid some iteration for the holiday search if you use the -contains operator.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 40629440
In the following example, the $d variable, contains a datetime value.
Example:
$holidays -contains (get-date -date $d.adddays(-1) -uformat "%Y-%m-%d")

Open in new window


You can separate some of these functions, updating your $d variable, as you step back in time.
$d = $d.adddays(-1)
$holidays -contains (get-date -date $d -uformat "%Y-%m-%d")

Open in new window

0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 65

Expert Comment

by:RobSampson
ID: 40629461
I haven't tested this too much, but you can also do away with the recursive call by using a loop that rechecks the date.

Rob.

$holidays = "2013-11-28","2013-12-25","2014-01-01","2014-01-20","2014-02-17","2014-04-18","2014-05-26","2014-07-04","2014-09-01","2014-02-17","2014-11-27","2014-12-25","2014-01-01","2014-01-19"

Function GetWorkDay($procDate)
{
    $recheck = $true
	While ($recheck -eq $true)
    {
        $recheck = $false
        $dow = (Get-Date($procDate)).DayOfWeek
	    $procDate = Switch ($dow)
	    {
		    "Saturday" { (Get-Date($procDate)).AddDays(-1).ToString("yyyy-MM-dd") }
		    "Sunday" { (Get-Date($procDate)).AddDays(-2).ToString("yyyy-MM-dd") }
		    "Monday" { (Get-Date($procDate)).AddDays(-3).ToString("yyyy-MM-dd") }
		    default { (Get-Date($procDate)).AddDays(-1).ToString("yyyy-MM-dd") }
	    }
        If ($holidays -contains $procDate)
	    {
            $procDate = (Get-Date($procDate)).AddDays(-1)
            $recheck = $true
	    }
    }
	Return $procDate
}

Open in new window

0
 
LVL 15

Author Closing Comment

by:dbbishop
ID: 40629830
Had I not been working 50-60 hours/week for the last couple of weeks on a project that has everyone breathing down my neck, I might of figured that one out, because I think I did something similar awhile back. But, I gotta love EE. Thanks!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 40629835
No problem, we're here to help (and learn)!  ;-)

Rob.
0

Featured Post

Four New Appliances. Same Industry-leading Speeds.

But don't take it from us.  The Firebox M370 is Miercom tested and Miercom approved, outperforming its competitors for stateless and stateful traffic throughput scenarios.  Learn more about the M370, M470, M570 and M670 and find the right solution for your organization today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question