I have a csv of of users with a date that they’re scheduled for some activity.
samaccountname Date
User1 20/09/2017
User2 21/09/2017
User3 23/09/2017
User4 20/09/2017
User5
User6
User7 21/09/2017
User8
User9 22/09/2017
User10
I’m looking to create a script that will find
the users who don’t have a date
Preselect a date 1 week into the future
Check if less than 2 users are scheduled on that Preselected date
If less than 2 users are scheduled, assign a user to that date
If more than 2 users are scheduled, find the next available date
The part I’m struggling with is the looping of the schedule to find an available date
SAmple code is
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" | 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 {$_.date -ne ""} | Select-Object *,@{ 'Name' = 'SchedDate' 'Expression' = { ([datetime]::ParseExact($_.date,"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) { If ($1WeekAfterToday.Date -eq (Get-date $datum.Name).Date ) { IF ($datum.Count -lt 2) { (Get-date $datum.Name) break } $1WeekAfterToday = $1WeekAfterToday.AddDays(1) continue } ELSE { $1WeekAfterToday #Break } #> } } Until ($avail) $avail | Write-Host -ForegroundColor Yellow
The fact that I’m comparing datetime objects makes it a lot more tricky!