I have a csv file that contains the following details
samaccountname,extensionattribute5
User01,22/09/2017
User02,22/09/2017
User03,22/09/2017
User04,23/09/2017
User05,23/09/2017
User06,24/09/2017
User07,24/09/2017
User08,24/09/2017
User09,25/09/2017
User10,25/09/2017
User11,
User12,
User13,
I’m looking for the ability for the script to assign an available date (no earlier than a week) to those that don’t have Extensionattribute5 populated.
The day cannot have more than 3 users assigned
The script needs to:
Lists all the dates scheduled.
Group them
Checks if there are less than 3 users.
If yes, then get the number of free slots, assign that date to the number of users.
If no, find the next available date
Remove-Variable *
cls
Set-Location $PSScriptRoot
$1WeekAfterToday = (Get-date ).AddDays(8)
If ($1WeekAfterToday.DayOfWeek -eq 'Saturday')
{
"Scheduled date is Saturday adding one more day" | Write-Host -ForegroundColor Cyan
$1WeekAfterToday = $1WeekAfterToday.AddDays('1')
}
ElseIf ($1WeekAfterToday.DayOfWeek -eq 'Friday')
{
"Scheduled date is Friday scheduling for Sunday" | Write-Host -ForegroundColor Cyan
$1WeekAfterToday = $1WeekAfterToday.AddDays('2')
}
$1WeekAfterToday = Get-Date $1WeekAfterToday -DisplayHint Date
"Potential date is $($1WeekAfterToday.ToLongDateString())" | Write-Host -ForegroundColor Cyan
#Get Users in Group
$sourceUsers = Import-Csv .\sourceUsers.csv -Verbose
Start-Sleep -Seconds 1 -Verbose
#Get Users that are scheduled
$PreScheduledUsers = Import-Csv .\sourceUsers.csv | Where-Object {$_.extensionattribute5 -ne ""} |
Select-Object *,@{
'Name' = 'SchedDate'
'Expression' = { ([datetime]::ParseExact($_.extensionattribute5,"dd/MM/yyyy",[System.Globalization.CultureInfo]::InvariantCulture)) }
} |
Sort SchedDate
#Group Dates
$AllDates = $PreScheduledUsers | Group SchedDate
#Check if date is already in schedule
#DO {
Sleep 1
#$Avail =
ForEach($datum in $AllDates)
{
Write-Host "`r`n01. CHECKING $($1WeekAfterToday.ToShortDateString()) to $((Get-date $datum.Name).ToShortDateString())"
If ($1WeekAfterToday.Date -eq (Get-date $datum.Name).Date )
{
Write-Host "`r`nFOUND A MATCH for $($1WeekAfterToday.Date)"
IF ($datum.Count -lt 3)
{
Write-Host "LESS than 3"
(Get-date $datum.Name)
break
}
ELSE
{
Write-Host "`r`nDATE NOt available less than ..checking next day"
$1WeekAfterToday = $1WeekAfterToday.AddDays(1)
continue
}#continue
#Break
}
ELSE
{
Write-Host "`r`nNo MATCH for $($1WeekAfterToday.Date). Setting $1WeekAfterToday"
$1WeekAfterToday
#Break
continue
}
#>
}
# }
#Until ($avail)
$avail | Write-Host -ForegroundColor Yellow
The aim is to find the earliest available date after a week from today.
So for eg if today is the 22nd of Sept 2017
It will check 29th Sept if there is less than 3 allocations. If so 29th Sept becomes the allocated date for that user.
If 29th is NOT available, it will increment the date by a day and check 30th Sept if less than 3 allocations and so on.
@ Sam Boutros thanks however I’m hoping to achieve this comprehensively.