Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

How to add parameter to function counting working days in powershell?

$
0
0
Hello, This function is counting working days from specified date back in time. The question is : What is the proper way to add
parameter to this function so this function can be used for counting days in different countries.

Example :
Get-WorkingDay 5 "2015-01-01" Canada

Function is counting 5 working days from date "2015-01-01" using Canadian list of holidays.
If the parameter for holidays is not used, default setting should be US holiday.





##################################################################################

$Canada = @( (Get-Date -Date '2014-01-01'), # New_Years_Day_Canada2014 (Get-Date -Date '2014-02-17'), # Family Day2014 (Get-Date -Date '2014-04-18'), # Good Friday_Canada2014 (Get-Date -Date '2014-05-19'), # Victoria Day2014 (Get-Date -Date '2014-07-01'), # Canada Day2014 (Get-Date -Date '2014-08-04'), # Civic Holiday2014 (Get-Date -Date '2014-09-01'), # Labour Day2014 (Get-Date -Date '2014-10-13'), # Thanksgiving Day_Canada2014 (Get-Date -Date '2014-12-25'), # Christmas Day_Canada2014 (Get-Date -Date '2014-12-26'), # Boxing Day2014 (Get-Date -Date '2015-01-01'), # New_Years_Day_Canada2015 (Get-Date -Date '2015-02-16'), # Family Day2015 (Get-Date -Date '2015-04-03'), # Good Friday_Canada2015 (Get-Date -Date '2015-05-18'), # Victoria Day2015 (Get-Date -Date '2015-07-01'), # Canada Day2015 (Get-Date -Date '2015-08-03'), # Civic Holiday2015 (Get-Date -Date '2015-09-07'), # Labour Day2015 (Get-Date -Date '2015-10-12'), # Thanksgiving Day_Canada2015 (Get-Date -Date '2015-12-25'), # Christmas Day_Canada2015 (Get-Date -Date '2015-12-28') # Boxing Day2015 ############################################################################# Function Get-WorkingDay{ param( [Parameter(Mandatory=$True,Position=1)] [int]$maxWorkingDays, [Parameter(Mandatory=$False,Position=2)] [datetime]$startDate = (Get-Date).Date ) $holidays = @( (Get-Date -Date '2014-01-01'), # New_Years_Day2014 (Get-Date -Date '2014-01-20'), # Martin_Luther_King2014 (Get-Date -Date '2014-02-17'), # Washingtons_Birthday2014 (Get-Date -Date '2014-04-18'), # Good_Friday2014 (Get-Date -Date '2014-05-26'), # Memorial_Day2014 (Get-Date -Date '2014-07-04'), # Independence_Day2014 (Get-Date -Date '2014-09-01'), # Labor_Day2014 (Get-Date -Date '2014-11-27'), # Thanksgiving_Day2014 (Get-Date -Date '2014-12-25'), # Christmas2014 (Get-Date -Date '2015-01-01'), # New_Years_Day2015 (Get-Date -Date '2015-01-19'), # Martin_Luther_King2015 (Get-Date -Date '2015-02-16'), # Washingtons_Birthday2015 (Get-Date -Date '2015-04-03'), # Good_Friday2015 (Get-Date -Date '2015-05-25'), # Memorial_Day2015 (Get-Date -Date '2015-07-03'), # Independence_Day2015 (Get-Date -Date '2015-09-07'), # Labor_Day2015 (Get-Date -Date '2015-11-26'), # Thanksgiving_Day2015 (Get-Date -Date '2015-12-25') # Christmas2015 ) # Count down the working days checking each date as we progress $dateIndex = $startDate For($workingDayIndex = $maxWorkingDays; $workingDayIndex -gt 0; $workingDayIndex--){ # Assume the current day is not a working day $isWorkingDay = $False Do{ If (("Sunday","Saturday" -contains $dateIndex.DayOfWeek) -or ($holidays -contains $dateIndex)){ # This is not a working day. Check the next day. # Write-Host "$($dateIndex.Date) is a weekend or holiday" -ForegroundColor Red $dateIndex = $dateIndex.AddDays(-1) } Else { # Current $dateIndex is a working day. $isWorkingDay = $True } } While(!$isWorkingDay) # Write-Host "Current Date: $($dateIndex.Date). Number of working days left: $workingDayIndex." # Set the $dateIndex to the previous day. $dateIndex = $dateIndex.AddDays(-1) } # The last date was the correct one. Re-add the day. $dateIndex.AddDays(1) } Get-WorkingDay 5

Viewing all articles
Browse latest Browse all 6937

Trending Articles