Calculate the first working day after N vacation days

…this post was published on 2015 October 20.

This time , we calculate the first working day after N free days. The script need the .csv file as in the previous post.

You should provide the file’s name, start-date  and the number of free days

<# .Synopsis Get total number of week days .Description Return the number of days between two dates not counting Saturday and Sunday. .Parameter File The csv file with Hollydays .Parameter Start The starting date .Parameter Days The number of free days .Example PS C:\> Get-TotalWeekDays -file ./Hollydays.csv -start 7/1/2012 -days 20
22
.Inputs
None
.Outputs
Integer
.Author
Romeo Donca mail:romeodonca@outlook.com
#>
[cmdletbinding()]
Param (
[Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$true,HelpMessage=”what is the csv file name?”)]
[ValidateNotNullorEmpty()]
[ValidatePattern(‘(.csv)$’)]
[String]$file,
[Parameter(Position=1,Mandatory=$True,ValueFromPipeline=$true,HelpMessage=”What is the start date?”)]
[ValidateNotNullorEmpty()]
[DateTime]$Start,
[Parameter(Position=2,Mandatory=$True,ValueFromPipeline=$true,HelpMessage=”What is the number of free days?”)]
[ValidateNotNullorEmpty()]
[int]$Days
)
Function Get_LastHollyDay()

#read the csv file and store it in $hollydays
$hollydays = Import-Csv $file
#define new array with working days from hollydays
$notworkingdays=@()
#get this array’s elements
$notworkingdaysnumber =$notworkingdays.Length
foreach ($data in $hollydays){
#$data
if (([datetime]$data.date).DayOfWeek -notmatch “Sunday|Saturday”){
$notworkingdays += $data.date
$notworkingdaysnumber = $notworkingdays.Length
}
}
#define a counter
$i=1
$end=$start
$days=$days+1
# Add day by day to Start value
do
{
if ( ($notworkingdays -contains (get-date $end -Format MM.dd.yyyy)) -match $false){
$i=$i+1
}
if ( $end.Date.DayOfWeek -match “Sunday|Saturday”){
$i=$i-1
}
$end =$end.AddDays(1)
}
while ($i -le $days)
$end =$end.AddDays(-1)
cls
“”
“”
“********************************************************”
“”
” First Vacation Day: $($start.ToShortDateString())”
” First Working Day:$($end.DayOfWeek) – $($end.ToShortDateString())”
“”
“********************************************************”
} #end function
Get_LastHollyDay
This entry was posted in Coding, Powershell. Bookmark the permalink.