Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PowerShell Code - Is a date in this Array/HashTable

Posted on 2015-02-24
6
Medium Priority
?
184 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
  • 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
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 anti-spam), the admin…

810 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