This commit is contained in:
2025-10-14 16:56:50 +02:00
parent b0f349311a
commit 09b74545ca

View File

@@ -27,7 +27,7 @@ function Write-Debug {
Write-Host $message
}
else {
$message | Add-Content ./log.txt
$message | Add-Content $logPath
}
}
@@ -40,55 +40,44 @@ function Set-Shift {
[System.DateTime]$StartDate,
[System.DateTime]$EndDate,
[string]$color,
[string]$teamID
[string]$teamID,
[string]$mail
)
$params = @{
UserId = $userId
schedulingGroupId = $groupID
sharedShift = @{
notes = ($shiftName)
startDateTime = [System.DateTime]::Parse($StartDate.ToString("yyyy-MM-dd'T'HH:mm:ssZ"))
endDateTime = [System.DateTime]::Parse($EndDate.ToString("yyyy-MM-dd'T'HH:mm:ssZ"))
theme = $color
}
}
#$params = @{
# UserId = $userId
# schedulingGroupId = $groupID
# sharedShift = @{
# notes = ($shiftName)
# startDateTime = [System.DateTime]::Parse($StartDate.ToString("yyyy-MM-dd'T'HH:mm:ssZ"))
# endDateTime = [System.DateTime]::Parse($EndDate.ToString("yyyy-MM-dd'T'HH:mm:ssZ"))
# theme = $color
# }
#}
#$headers = @{
# "MS-APP-ACTS-AS" = $userIdAdmin
# "Prefer" = "no-notification"
#}
$newShift = New-MgTeamScheduleShift -TeamId $teamID -DraftShift -BodyParameter $params -Headers @{ "MS-APP-ACTS-AS" = $userIdAdmin }
Write-Debug ("SCHEDULING: {0} {1} - {2}" -f $newShift.SharedShift.StartDateTime.ToString("yyyy-MM-dd HH:mm"), $newShift.SharedShift.EndDateTime.ToString("yyyy-MM-dd HH:mm"), $newShift.SharedShift.Notes)
#$newShift = New-MgTeamScheduleShift -TeamId $teamID -DraftShift -BodyParameter $params -Headers @{ "MS-APP-ACTS-AS" = $userIdAdmin }
#Write-Debug ("SCHEDULING: {0} {1} - {2}" -f $newShift.SharedShift.StartDateTime.ToString("yyyy-MM-dd HH:mm"), $newShift.SharedShift.EndDateTime.ToString("yyyy-MM-dd HH:mm"), $newShift.SharedShift.Notes)
#("SCHEDULING: {0} {1} - {2}" -f $newShift.SharedShift.StartDateTime.ToString("yyyy-MM-dd HH:mm"), $newShift.SharedShift.EndDateTime.ToString("yyyy-MM-dd HH:mm"), $newShift.SharedShift.Notes) | Add-Content -Path $logPath
Write-Debug("{0} {1}: {2} {3}" -f $dayDate, $dayDate.DayOfWeek, $mail, $shiftName)
$newShift = 0
return $newShift
}
#Kontrola, zda má uvedené ID nějakou ze směn v množině
function Invoke-HasShift {
param(
[string]$userID,
[PSCUstomObject]$TestedShifts
)
foreach ($shift in $TestedShifts) {
if ($shift.UserId -eq $userID) {
return $true
break
}
}
return $false
}
function Invoke-HasTimeOff {
param(
[string]$userID,
[PSCustomObject]$TestedTimeOff
[string]$UID,
[PSCustomObject]$timeoff,
[string]$mail
)
foreach ($time_off in $TestedTimeOff) {
if ($time_off.UserId -eq $userID) {
foreach ($toff in $timeoff) {
if($toff.UserId -eq $UID) {
Write-Debug("{0} {1}: {2} has time off at this time" -f $dayDate, $dayDate.DayOfWeek, $mail)
return $true
break
}
@@ -96,25 +85,47 @@ function Invoke-HasTimeOff {
return $false
}
function Invoke-CanHaveOnSite {
function Invoke-HasShift {
param(
[string]$UID,
[PSCustomObject]$shifts,
[string]$mail
)
foreach ($shift in $shifts) {
if($shift.UserId -eq $UID) {
Write-Debug("{0} {1}: {2} has a shift at this time" -f $dayDate, $dayDate.DayOfWeek, $mail)
return $true
break
}
}
return $false
}
function Invoke-HasShiftorTimeOff {
param(
[string]$UID,
[PSCustomObject]$shifts,
[PSCustomObject]$timeoff,
[string]$mail
)
function Invoke-CanHaveDayShift {
foreach ($shift in $shifts) {
if($shift.UserId -eq $UID) {
Write-Debug("{0} {1}: {2} has a shift or time off at this time" -f $dayDate, $dayDate.DayOfWeek, $mail)
return $true
break
}
}
function Invoke-CanHaveNoShift {
foreach ($toff in $timeoff) {
if($toff.UserId -eq $UID) {
Write-Debug("{0} {1}: {2} has a shift or time off at this time" -f $dayDate, $dayDate.DayOfWeek, $mail)
return $true
break
}
function Invoke-CanHaveHomeOffice {
}
function Invoke-CanHaveOffice {
return $false
}
#Microsoft auth
@@ -137,7 +148,7 @@ Connect-MgGraph -AccessToken $($token | ConvertTo-SecureString -AsPlainText -For
#$today = (Get-Date).Date
#$today = $today_old.AddYears(2)
$today = Get-Date -Day 18 -Month 1 -Year 2027 -Hour 9 -Minute 00 -Second 00 -Millisecond 00
$today = Get-Date -Day 11 -Month 1 -Year 2027 -Hour 9 -Minute 00 -Second 00 -Millisecond 00
$schedule = Get-Content -Path "./config.json" -Raw | ConvertFrom-Json
@@ -155,20 +166,17 @@ $allemails = $schedule.PSObject.Properties | ForEach-Object {
#Časové rozmezí plánování
$Monday = $today.AddDays(1 - $today.DayOfWeek.value__)
$Friday = $Monday.AddDays(4)
$daysahead = 5
#$Monday = $today.AddDays(1 - $today.DayOfWeek.value__)
#$Friday = $Monday.AddDays(4)
$daysahead = 14
$startSpanDate = Get-Date -Day $today.Day -Month $today.Month -Year $today.Year -Hour 9 -Minute 00 -Second 00 -Millisecond 00
$endSpanDate = Get-Date -Day $startSpanDate.AddDays($daysahead).Day -Month $startSpanDate.AddDays($daysahead).Month -Year $startSpanDate.AddDays($daysahead).Year -Hour 17 -Minute 00 -Second 00 -Millisecond 00
#RLCZ maintenance
$rlcz_mtnc_email = "jiri.kotlan@itego.cz"
$rlcz_mtnc_userId = $(Get-MgUser -Filter "UserPrincipalName eq '$rlcz_mtnc_email' or proxyAddresses/any(c:c eq 'smtp:$rlcz_mtnc_email')").Id
$rlcz_shift_reduction = -3
Write-Host $startSpanDate
Write-Host $endSpanDate
# SKRIPT
try {
@@ -176,11 +184,12 @@ try {
$group = Get-MgTeamScheduleSchedulingGroup -TeamId $team.id -Headers @{ "MS-APP-ACTS-AS" = $userIdAdmin } | Where-Object -property displayName -value $scheduleGroupName -eq
$allshifts = [object[]] (Get-MgTeamScheduleShift -TeamId $team.id -Filter "SharedShift/StartDateTime ge $($startSpanDate.toString('yyyy-MM-ddT00:00:00Z'))" -All -Headers @{ "MS-APP-ACTS-AS" = $userIdAdmin })
$timeoff = [object[]] (Get-MgTeamScheduleTimeOff -TeamId $team.id -Filter "SharedTimeOff/StartDateTime ge $($startSpanDate.toString('yyyy-MM-ddT00:00:00Z'))" -All -Headers @{ "MS-APP-ACTS-AS" = $userIdAdmin } | Select-Object -Property *,@{name="Date";expression={$_.SharedTimeOff.StartDateTime.AddHours(1).Date}})
$timeoff_all = [object[]] (Get-MgTeamScheduleTimeOff -TeamId $team.id -Filter "SharedTimeOff/StartDateTime ge $($startSpanDate.toString('yyyy-MM-ddT00:00:00Z'))" -All -Headers @{ "MS-APP-ACTS-AS" = $userIdAdmin } | Select-Object -Property *,@{name="Date";expression={$_.SharedTimeOff.StartDateTime.AddHours(1).Date}})
$timeoff_now = $timeoff | Where-Object -Property Date -eq -Value $(Get-Date -Day 20 -Month 1 -Year 2027).Date
#return
#$timeoff_today = $timeoff_all | Where-Object -Property Date -eq -Value $(Get-Date -Day 20 -Month 1 -Year 2027).Date
return $timeoff_now
#if ($clear) {
# foreach ($shift in $allshifts) {
@@ -193,7 +202,7 @@ try {
#Procházíme dny v časovém rozmezí
$timeSpan = New-TimeSpan -Start $startSpanDate -End $endSpanDate
Write-Debug ("Setting schedule from {0} to {1}" -f $startSpanDate, $endSpanDate)
Write-Debug("Planning from {0} to {1}" -f $startSpanDate, $endSpanDate)
for ($dayNumber = 0; $dayNumber -lt $timespan.Days; $dayNumber++) {
$dayDate = $today.AddDays($dayNumber);
@@ -201,14 +210,12 @@ try {
if ($dayDate.DayOfWeek -notin @("Saturday", "Sunday")) {
#Inicializace dne
$ds_shift_inplace = $false
$ds_skipped = $false
#Množina emailů pro tento den v týdnu co mají mít home office
$ho_emails = $schedule.($dayDate.DayOfWeek.ToString().ToLower()).'ho'
#Email a ID toho kdo má mít tento den denní směnu
$ds_email = $schedule.($dayDate.DayOfWeek.ToString().ToLower()).'ds'
$ds_userId = $(Get-MgUser -Filter "UserPrincipalName eq '$ds_email' or proxyAddresses/any(c:c eq 'smtp:$ds_email')").Id
#Emaily těch co jsou on-site u zákazníka
$os_emails = $schedule.($dayDate.DayOfWeek.ToString().ToLower()).'os'
@@ -226,132 +233,149 @@ try {
$dateEnd = $(get-date -Day $dayDate.Day -Month $dayDate.Month -Year $dayDate.Year -Hour 17 -Minute 00 -Second 00).AddHours(-2)
}
$shifts_today = [Object[]] $allshifts | Where-Object -Filter { $_.schedulingGroupId -eq $group.Id -and $_.SharedShift.StartDateTime.ToString("yyyy-MM-dd HH:mm") -ge $dateStart.ToString("yyyy-MM-dd HH:mm") -and $_.SharedShift.EndDateTime.ToString("yyyy-MM-dd HH:mm") -le $dateEnd.ToString("yyyy-MM-dd HH:mm") }
$timeoff_today = [Object[]] $timeoff_all | Where-Object -Filter { $_.schedulingGroupId -eq $group.Id -and $_.SharedTimeOff.StartDateTime.ToString("yyyy-MM-dd HH:mm") -ge $dateStart.ToString("yyyy-MM-ddT00:00:00Z") -and $_.SharedTimeOff.EndDateTime.ToString("yyyy-MM-dd HH:mm") -le $dateEnd.ToString("yyyy-MM-ddT00:00:00Z") }
# Tohle jsou všechny způsoby co jsem zkoušel, nikdy to nematchne, nikdy to nevidí dovolenou
#$timeoff_today = [Object[]] $timeoff_all | Where-Object -Filter { $_.schedulingGroupId -eq $group.Id -and $_.SharedTimeOff.StartDateTime.ToString("yyyy-MM-dd HH:mm") -le $dateEnd.AddDays(1).ToString("yyyy-MM-dd HH:mm") -and $_.SharedTimeOff.EndDateTime.ToString("yyyy-MM-dd HH:mm") -ge $dateStart.AddDays(-1).ToString("yyyy-MM-dd HH:mm") }
#$timeoff_today = [Object[]] $timeoff_all | Where-Object { $_.schedulingGroupId -eq $group.Id -and $_.SharedTimeOff.StartDateTime -lt $dateEnd -and $_.SharedTimeOff.EndDateTime -gt $dateStart }
#$timeoff_today = $timeoff_all | Where-Object { $_.schedulingGroupId -eq $group.Id -and ([datetime]$_.SharedTimeOff.StartDateTime) -lt $dayEnd -and ([datetime]$_.SharedTimeOff.EndDateTime) -gt $dayStart }
#$timeoff_today = [Object[]] $timeoff_all | Where-Object -Filter { $_.schedulingGroupId -eq $group.Id -and $_.SharedTimeOff.StartDateTime.ToString("yyyy-MM-dd HH:mm") -ge $dateStart.ToString("yyyy-MM-ddT00:00:00Z") -and $_.SharedTimeOff.EndDateTime.ToString("yyyy-MM-dd HH:mm") -le $dateEnd.AddDays(1).ToString("yyyy-MM-ddT00:00:00Z") }
#$timeoff_today = [Object[]] $timeoff_all | Where-Object -Filter { $_.schedulingGroupId -eq $group.Id -and $_.SharedTimeOff.StartDateTime.ToString("yyyy-MM-dd HH:mm") -ge $dateStart.ToString("yyyy-MM-ddT00:00:00Z") -and $_.SharedTimeOff.EndDateTime.ToString("yyyy-MM-dd HH:mm") -le $dateEnd.ToString("yyyy-MM-ddT00:00:00Z") }
#Pokus o naplnění množiny manuálně daných denních směn pro tento den
$manual_dayshifts = [Object[]] $shifts_today | Where-Object -Filter { ($_.SharedShift.Notes -eq "Home Office - Denní směna") -or ($_.SharedShift.Notes -eq "Office - Denní směna") }
if ($null -eq $manual_dayshifts) {
$ds_shift_inplace = $false
Write-Debug("{0} {1}: day shift not set" -f $dayDate, $dayDate.DayOfWeek)
foreach($shift in $manual_dayshifts) {
Write-Debug("{0}: {1}-{2}"-f $shift.SharedShift.Notes, $shift.SharedShift.StartDateTime, $shift.SharedShift.EndDateTime)
}
}
else {
$ds_shift_inplace = $true
Write-Debug("{0} {1}: day shift set already" -f $dayDate, $dayDate.DayOfWeek)
foreach($shift in $manual_dayshifts) {
Write-Debug("{0}: {1}-{2}"-f $shift.SharedShift.Notes, $shift.SharedShift.StartDateTime, $shift.SharedShift.EndDateTime)
}
}
foreach($email in $allemails) {
$userId = $(Get-MgUser -Filter "UserPrincipalName eq '$email' or proxyAddresses/any(c:c eq 'smtp:$email')").Id
if($ds_shift_inplace -eq $false) {
if($email -eq $ds_email) {
if(((Invoke-HasShift -userID $userId -TestedShifts $shifts_today) -eq $false) -and ((Invoke-HasTimeOff -userID $userId -TestedTimeOff $timeoff_today) -eq $false)) {
Write-Debug("{0} {1}: checking {2} for day shift availability..." -f $dayDate, $dayDate.DayOfWeek, $email)
foreach($toff in $timeoff_today) {
if($toff.UserId -eq $userId) {
Write-Debug("{0}: {1} {2}" -f $email, $toff.SharedTimeOff.StartDateTime, $toff.SharedTimeOff.EndDateTime)
}
}
if((Invoke-HasShiftorTimeOff -UID $userId -shifts $shifts_today -timeoff $timeoff_today -mail $email) -eq $false) {
if ($email -in $ho_emails) {
$newshift = Set-Shift -userId $userId -groupID $group.id -shiftName "Home Office - Denní směna" -StartDate $dateStart -EndDate $dateEnd -color "purple" -teamID $team.id
$newshift = Set-Shift -userId $userId -groupID $group.id -shiftName "Home Office - Denní směna" -StartDate $dateStart -EndDate $dateEnd -color "purple" -teamID $team.id -mail $email
$allshifts += [Object[]] $newshift
$ds_shift_inplace = $true
continue
}
else {
$newshift = Set-Shift -userId $userId -groupID $group.id -shiftName "Office - Denní směna" -StartDate $dateStart -EndDate $dateEnd -color "purple" -teamID $team.id
$newshift = Set-Shift -userId $userId -groupID $group.id -shiftName "Office - Denní směna" -StartDate $dateStart -EndDate $dateEnd -color "purple" -teamID $team.id -mail $email
$allshifts += [Object[]] $newShift
$ds_shift_inplace = $true
continue
}
}
else {
#Budeme hledat ve zbývajících dnech zvoleného rozsahu
Write-Debug("{0} {1}: looking ahead for a day shift replacement" -f $dayDate, $dayDate.DayOfWeek)
$remainingdays = $timespan.Days - $dayNumber
for ($daysahead = 1; $daysahead -lt $remainingdays; $daysahead++) {
$futureday = $dayDate.AddDays($daysahead)
#Pokud jsme narazili na pracovní den...
if ($futureday.DayOfWeek -notin @("Saturday", "Sunday")) {
#...jdeme se podívat kdo má mít denní směnu
$futureds_email = $schedule.($futureday.DayOfWeek.ToString().ToLower()).'ds'
Write-Debug(">>> {0} {1}: {2} has it on that day" -f $futureday, $futureday.DayOfWeek, $futureds_email)
$futureds_id = $(Get-MgUser -Filter "UserPrincipalName eq '$futureds_email' or proxyAddresses/any(c:c eq 'smtp:$futureds_email')").Id
#Má další na řadě nastavenou na dnešek směnu? Pokud ne tak má denní a hledání ukončujeme, jinak se jde na další den
if((Invoke-HasTimeOff -UID $futureds_id -timeoff $timeoff_today -mail $futureds_email) -eq $false -and ($futureds_email -notin $os_emails) -and ($futureds_email -notin $ns_emails)) {
Write-Debug(">>> {0} {1}: {2} doesn't have time off, on site or no-shift on {3} {4}" -f $futureday, $futureday.DayOfWeek, $futureds_email, $dayDate, $dayDate.DayOfWeek)
if((Invoke-HasShift -UID $futureds_id -shifts $shifts_today -mail $futureds_email) -eq $false) {
Write-Debug(">>> {0} {1}: {2} ID: {3} doesn't have a shift on {4} {5}" -f $futureday, $futureday.DayOfWeek, $futureds_email, $futureds_id, $dayDate, $dayDate.DayOfWeek)
if($futureds_email -in $ho_emails) {
$newshift = Set-Shift -userId $futureds_id -groupID $group.id -shiftName "Home Office - Denní směna" -StartDate $dateStart -EndDate $dateEnd -color "purple" -teamID $team.id -mail $futureds_email
$allshifts += [Object[]] $newshift
$ds_shift_inplace = $true
break
}
else {
$newshift = Set-Shift -userId $futureds_id -groupID $group.id -shiftName "Office - Denní směna" -StartDate $dateStart -EndDate $dateEnd -color "purple" -teamID $team.id -mail $futureds_email
$allshifts += [Object[]] $newShift
$ds_shift_inplace = $true
break
}
}
else {
Write-Debug(">>> {0} {1}: {2} has a shift on {3} {4}" -f $futureday, $futureday.DayOfWeek, $futureds_email, $dayDate, $dayDate.DayOfWeek)
foreach ($shift in $shifts_today) {
if($shift.UserId -eq $futureds_id) {
Write-Debug(">>> {0} {1}: removing shift {2} {3} {4} for {5}" -f $futureday, $futureday.DayOfWeek, $shift.Notes, $shift.StartDateTime, $shift.EndDateTime, $futureds_email)
#Remove-MgTeamScheduleShift -ShiftId $shift.id -TeamId $team.Id
if($futureds_email -in $ho_emails) {
$newshift = Set-Shift -userId $futureds_id -groupID $group.id -shiftName "Home Office - Denní směna" -StartDate $dateStart -EndDate $dateEnd -color "purple" -teamID $team.id -mail $futureds_email
$allshifts += [Object[]] $newshift
$ds_shift_inplace = $true
break
}
else {
$newshift = Set-Shift -userId $futureds_id -groupID $group.id -shiftName "Office - Denní směna" -StartDate $dateStart -EndDate $dateEnd -color "purple" -teamID $team.id -mail $futureds_email
$allshifts += [Object[]] $newShift
$ds_shift_inplace = $true
break
}
}
}
}
}
else {
foreach($timeoff in $timeoff_today) {
if($timeoff.UserId -eq $futureds_id) {
Write-Debug(">>> {0} {1}: {2} has a shift on {3} {4} - {5} {6} {7}" -f $futureday, $futureday.DayOfWeek, $futureds_email, $dayDate, $dayDate.DayOfWeek, $timeoff.SharedTimeOff.Notes, $timeoff.SharedTimeOff.StartDateTime, $timeoff.SharedTimeOff.EndDateTime)
}
}
if($futureds_email -in $os_emails) {
Write-Debug(">>> {0} {1}: {2} is on the on-site list {3} {4}" -f $futureday, $futureday.DayOfWeek, $futureds_email, $dayDate, $dayDate.DayOfWeek)
}
if($futureds_email -in $ns_emails) {
Write-Debug(">>> {0} {1}: {2} is on the no-shift list {3} {4}" -f $futureday, $futureday.DayOfWeek, $futureds_email, $dayDate, $dayDate.DayOfWeek)
}
}
}
}
}
}
}
if($email -in $os_emails) {
if(((Invoke-HasShift -userID $userId -TestedShifts $shifts_today) -eq $false) -and ((Invoke-HasTimeOff -userID $userId -TestedTimeOff $timeoff_today) -eq $false)) {
$newshift = Set-Shift -userId $userId -groupID $group.id -shiftName "On-site" -StartDate $dateStart -EndDate $dateEnd -color "gray" -teamID $team.id
if((Invoke-HasShiftorTimeOff -UID $userId -shifts $shifts_today -timeoff $timeoff_today -mail $email) -eq $false) {
$newshift = Set-Shift -userId $userId -groupID $group.id -shiftName "On-site" -StartDate $dateStart -EndDate $dateEnd -color "gray" -teamID $team.id -mail $email
$allshifts += [Object[]] $newshift
continue
}
}
if($email -in $ns_emails) {
continue
}
#Všechny směny které jsou v rozmezí 9:00-17:00 hodin tento den
$shifts_today = [Object[]] $allshifts | Where-Object -Filter { $_.schedulingGroupId -eq $group.Id -and $_.SharedShift.StartDateTime.ToString("yyyy-MM-dd HH:mm") -ge $dateStart.ToString("yyyy-MM-dd HH:mm") -and $_.SharedShift.EndDateTime.ToString("yyyy-MM-dd HH:mm") -le $dateEnd.ToString("yyyy-MM-dd HH:mm") }
$timeoff_today = [Object[]] $timeoff | Where-Object -Filter { $_.schedulingGroupId -eq $group.Id -and $_.SharedTimeOff.StartDateTime.ToString("yyyy-MM-dd HH:mm") -ge $dateStart.ToString("yyyy-MM-dd HH:mm") -and $_.SharedTimeOff.EndDateTime.ToString("yyyy-MM-dd HH:mm") -le $dateEnd.ToString("yyyy-MM-dd HH:mm") }
#Pokus o naplnění množiny manuálně daných denních směn pro tento den
$manual_dayshifts = [Object[]] $shifts_today | Where-Object -Filter { { $_.SharedShift.Notes -eq "Home Office - Denní směna" -or $_.SharedShift.Notes -eq "Office - Denní směna" } }
#Pokud je tato množina prázdná, tak dnes nebyla manuálně nastavena denní směna
if ($null -eq $manual_dayshifts) {
$ds_shift_inplace = $false
Write-Debug ("{0}: Day-shift not yet set" -f $dayDate)
}
else {
$ds_shift_inplace = $true
Write-Debug ("{0}: Day-shift is already set" -f $dayDate)
}
#Pokud nebyla nikomu manuálně nastavena denní směna
if ($ds_shift_inplace -eq $false) {
#Má ten kdo má mít denní směnu ve stejný den už nějakou směnu?
if (((Invoke-HasShift -userID $ds_userId -TestedShifts $shifts_today) -eq $true) -or ((Invoke-HasTimeOff -userID $ds_userId -TestedTimeOff $timeoff_today) -eq $true)) {
#Pokud ano, je to směna co má mít? Teoreticky by nemělo nastat, mělo by již být odchyceno v kontrole manuálních směn
if (Invoke-HasShift -userID $ds_userId -TestedShifts ($shifts_today | Where-Object -Filter { $_.SharedShift.Notes -eq "Home Office - Denní směna" -or $_.SharedShift.Notes -eq "Office - Denní směna" }) -eq $true) {
$ds_shift_inplace = $true
Write-Debug("{0}: {1} already has the day-shift" -f $dayDate, $ds_email)
}
#Není to denní směna, je manuálně daná jiného typu (může být i volno), bude potřeba najít náhradníka
else {
$ds_skipped = $true
$ds_shift_inplace = $false
Write-Debug("{0}: {1} can't have the day-shift" -f $dayDate, $ds_email)
}
}
#Ten co má mít denní směnu nemá ve stejný čas ještě žádnou směnu
else {
Write-Debug("{0}: {1} can have the day-shift" -f $dayDate, $ds_email)
$ds_shift_inplace = $false
$ds_skipped = $false
}
}
#Pokud bylo zjištěno, že má denní směna naplánovanou jinou směnu (ne-denní, nebo volno...) a je potřeba najít náhradníka
if ($ds_skipped -eq $true) {
#Budeme hledat ve zbývajících dnech zvoleného rozsahu
$remainingdays = $timespan.Days - $dayNumber
Write-Debug("{0}: looking for the next in line to have the day-shift" -f $dayDate)
for ($daysahead = 1; $daysahead -lt $remainingdays; $daysahead++) {
$futureday = $dayDate.AddDays($daysahead)
Write-Debug("Checking {0} - {1}" -f $futureday, $futureday.DayOfWeek)
#Pokud jsme narazili na pracovní den...
if ($futureday.DayOfWeek -notin @("Saturday", "Sunday")) {
#...jdeme se podívat kdo má mít denní směnu
$futureds_email = $schedule.($futureday.DayOfWeek.ToString().ToLower()).'ds'
$futureds_id = $(Get-MgUser -Filter "UserPrincipalName eq '$futureds_email' or proxyAddresses/any(c:c eq 'smtp:$futureds_email')").Id
Write-Debug("{0} is next in line for the day-shift" -f $futureds_email)
#Má další na řadě nastavenou na dnešek směnu? Pokud ne tak má denní a hledání ukončujeme, jinak se jde na další den
if (((Invoke-HasShift -userID $futureds_id -TestedShifts $shifts_today) -eq $false) -and ((Invoke-HasTimeOff -userID $futureds_id -TestedTimeOff $timeoff_today) -eq $false)) {
Write-Debug("{0} can have the day-shift" -f $futureds_email)
$ds_email = $futureds_email
$ds_userId = $futureds_id
break
}
else {
Write-Debug("{0} can't have the day-shift" -f $futureds_email)
}
}
}
}
#Pokud nebyla manuálně nastavena denní směna
if ($ds_shift_inplace -eq $false) {
#Pokud je emailová adresa toho kdo má denní směnu zároveň i v množině emailů co má home office tak je to home office denní směna
if ($ds_email -in $ho_emails) {
Write-Debug("{0}: {1} has the day-shift and home-office" -f $dayDate, $ds_email)
$newshift = Set-Shift -userId $ds_userId -groupID $group.id -shiftName "Home Office - Denní směna" -StartDate $dateStart -EndDate $dateEnd -color "purple" -teamID $team.id
$allshifts += [Object[]] $newshift
}
#Jinak je to denní směna v kanceláři
else {
Write-Debug("{0}: {1} has the day-shift" -f $dayDate, $ds_email)
$newshift = Set-Shift -userId $ds_userId -groupID $group.id -shiftName "Office - Denní směna" -StartDate $dateStart -EndDate $dateEnd -color "purple" -teamID $team.id
$allshifts += [Object[]] $newShift
}
}
$shifts_today = [Object[]] $allshifts | Where-Object -Filter { $_.schedulingGroupId -eq $group.Id -and $_.SharedShift.StartDateTime.ToString("yyyy-MM-dd HH:mm") -ge $dateStart.ToString("yyyy-MM-dd HH:mm") -and $_.SharedShift.EndDateTime.ToString("yyyy-MM-dd HH:mm") -le $dateEnd.ToString("yyyy-MM-dd HH:mm") }
#Pokud je čtvrtek, zjištujeme zda zkrátit směnu kvůli včerejší RLCZ maintenanci
if($email -eq $rlcz_mtnc_email) {
if ($dayDate.DayOfWeek -eq "Thursday") {
Write-Debug("{0}: It's thursday, checking if there was a RLCZ maintenance yesterday" -f $dayDate)
#Včerejší datum
$yesterday = $dayDate.AddDays(-1)
#Úprava času
if (($yesterday.IsDaylightSavingTime()) -eq $false) {
$rlcz_shift_start = $(get-date -Day $yesterday.Day -Month $yesterday.Month -Year $yesterday.Year -Hour 18 -Minute 00 -Second 00).AddHours(-1)
$rlcz_shift_end = $(get-date -Day $yesterday.Day -Month $yesterday.Month -Year $yesterday.Year -Hour 23 -Minute 00 -Second 00).AddHours(-1)
@@ -360,110 +384,51 @@ try {
$rlcz_shift_start = $(get-date -Day $yesterday.Day -Month $yesterday.Month -Year $yesterday.Year -Hour 18 -Minute 00 -Second 00).AddHours(-2)
$rlcz_shift_end = $(get-date -Day $yesterday.Day -Month $yesterday.Month -Year $yesterday.Year -Hour 23 -Minute 00 -Second 00).AddHours(-2)
}
#Včerejší RLCZ maintenance šichty
$rlcz_shifts = [Object[]] $allshifts | Where-Object -Filter { $_.schedulingGroupId -eq $group.Id -and $_.SharedShift.Notes -eq "RLCZ" -and $_.SharedShift.StartDateTime.ToString("yyyy-MM-dd HH:mm") -ge $rlcz_shift_start.ToString("yyyy-MM-dd HH:mm") -and $_.SharedShift.EndDateTime.ToString("yyyy-MM-dd HH:mm") -le $rlcz_shift_end.ToString("yyyy-MM-dd HH:mm") }
#Pokud byla včera RLCZ maintenance
if ((Invoke-HasShift -userID $rlcz_mtnc_userId -TestedShifts $rlcz_shifts) -eq $true) {
Write-Debug("{0}: RLCZ maintenance was yesterday, checking if {1} has any shifts set today" -f $dayDate, $rlcz_mtnc_email)
#Pokud nemá na dnes ještě naplánovanou směnu
if (((Invoke-HasShift -userID $rlcz_mtnc_userId -TestedShifts $shifts_today) -eq $false) -and (Invoke-HasTimeOff -userID $rlcz_mtnc_userId -TestedTimeOff $timeoff_today) -eq $false) {
Write-Debug("{0}: {1} doesn't have any shifts planned today" -f $dayDate, $rlcz_mtnc_email)
#Pokud má mít dnes home office
if ($rlcz_mtnc_email -in $ho_emails) {
Write-Debug("{0}: {1} is supposed to have home-office today" -f $dayDate, $rlcz_mtnc_email)
#Dostane zkrácenou home office směnu
$newshift = Set-Shift -userId $rlcz_mtnc_userId -groupID $group.id -shiftName "Home office" -StartDate $dateStart -EndDate $dateEnd.AddHours($rlcz_shift_reduction) -color "pink" -teamID $team.id
$allshifts += [Object[]] $newshift
}
#Pokud nemá mít home office
else {
Write-Debug("{0}: {1} isn't supposed to have home-office today" -f $dayDate, $rlcz_mtnc_email)
#Dostane zkrácenou office směnu
$newshift = Set-Shift -userId $rlcz_mtnc_userId -groupID $group.id -shiftName "Office" -StartDate $dateStart -EndDate $dateEnd.AddHours($rlcz_shift_reduction) -color "blue" -teamID $team.id
$allshifts += [Object[]] $newshift
}
}
}
}
$shifts_today = [Object[]] $allshifts | Where-Object -Filter { $_.schedulingGroupId -eq $group.Id -and $_.SharedShift.StartDateTime.ToString("yyyy-MM-dd HH:mm") -ge $dateStart.ToString("yyyy-MM-dd HH:mm") -and $_.SharedShift.EndDateTime.ToString("yyyy-MM-dd HH:mm") -le $dateEnd.ToString("yyyy-MM-dd HH:mm") }
#Pro každý email v množině emailů co mají home office
Write-Debug("{0}: checking who is supposed to have home-office" -f $dayDate)
foreach ($ho_email in $ho_emails) {
#Najdeme ID
$ho_userId = $(Get-MgUser -Filter "UserPrincipalName eq '$ho_email' or proxyAddresses/any(c:c eq 'smtp:$ho_email')").Id
Write-Debug("{0}: checking if {1} has a shift planned already" -f $dayDate, $ho_email)
#Pokud nemá již naplánovanou směnu, naplánujeme home office
if (((Invoke-HasShift -userID $ho_userId -TestedShifts $shifts_today) -eq $false) -and ((Invoke-HasTimeOff -userID $ho_userId -TestedTimeOff $timeoff_today) -eq $false)) {
Write-Debug("{0}: {1} doesn't have a shift planned already" -f $dayDate, $ho_email)
$newshift = Set-Shift -userId $ho_userId -groupID $group.id -shiftName "Home office" -StartDate $dateStart -EndDate $dateEnd -color "pink" -teamID $team.id
if (Invoke-HasShift -UID $userId -shifts $rlcz_shifts -eq $true -mail $email) {
if((Invoke-HasShiftorTimeOff -UID $userId -shifts $shifts_today -timeoff $timeoff_toda -mail $email) -eq $false) {
if($email -in $ho_emails) {
$newshift = Set-Shift -userId $userId -groupID $group.id -shiftName "Home office" -StartDate $dateStart -EndDate $dateEnd.AddHours($rlcz_shift_reduction) -color "pink" -teamID $team.id -mail $email
$allshifts += [Object[]] $newshift
continue
}
else {
Write-Debug("{0}: {1} has a shift planned already" -f $dayDate, $ho_email)
}
}
$shifts_today = [Object[]] $allshifts | Where-Object -Filter { $_.schedulingGroupId -eq $group.Id -and $_.SharedShift.StartDateTime.ToString("yyyy-MM-dd HH:mm") -ge $dateStart.ToString("yyyy-MM-dd HH:mm") -and $_.SharedShift.EndDateTime.ToString("yyyy-MM-dd HH:mm") -le $dateEnd.ToString("yyyy-MM-dd HH:mm") }
Write-Debug("{0}: checking who is supposed to have on-site" -f $dayDate)
foreach ($os_email in $os_emails) {
$os_userId = $(Get-MgUser -Filter "UserPrincipalName eq '$os_email' or proxyAddresses/any(c:c eq 'smtp:$os_email')").Id
Write-Debug("{0}: checking if {1} has a shift planned already" -f $dayDate, $os_email)
#Pokud nemá již naplánovanou směnu, naplánujeme on-site
if (((Invoke-HasShift -userID $os_userId -TestedShifts $shifts_today) -eq $false) -and ((Invoke-HasTimeOff -userID $os_userId -TestedTimeOff $timeoff_today) -eq $false)) {
Write-Debug("{0}: {1} doesn't have a shift planned already, setting on-site shift" -f $dayDate, $os_email)
$newshift = Set-Shift -userId $os_userId -groupID $group.id -shiftName "On-site" -StartDate $dateStart -EndDate $dateEnd -color "gray" -teamID $team.id
$newshift = Set-Shift -userId $userId -groupID $group.id -shiftName "Office" -StartDate $dateStart -EndDate $dateEnd.AddHours($rlcz_shift_reduction) -color "blue" -teamID $team.id -mail $email
$allshifts += [Object[]] $newshift
continue
}
}
}
else {
Write-Debug("{0}: {1} has a shift planned already, can't set on-site shift" -f $dayDate, $os_email)
}
}
$shifts_today = [Object[]] $allshifts | Where-Object -Filter { $_.schedulingGroupId -eq $group.Id -and $_.SharedShift.StartDateTime.ToString("yyyy-MM-dd HH:mm") -ge $dateStart.ToString("yyyy-MM-dd HH:mm") -and $_.SharedShift.EndDateTime.ToString("yyyy-MM-dd HH:mm") -le $dateEnd.ToString("yyyy-MM-dd HH:mm") }
Write-Debug("{0}: searching for whomever still doesn't have a shift, to give them an office shift" -f $dayDate)
foreach ($email in $allemails) {
$email_userId = $(Get-MgUser -Filter "UserPrincipalName eq '$email' or proxyAddresses/any(c:c eq 'smtp:$email')").Id
Write-Debug("{0}: checking if {1} has a shift planned already" -f $dayDate, $email)
#Pokud ještě nemá směnu...
if (((Invoke-HasShift -userID $email_userId -TestedShifts $shifts_today) -eq $false) -and ((Invoke-HasTimeOff -userID $email_userId -TestedTimeOff $timeoff_today) -eq $false)) {
Write-Debug("{0}: {1} doesn't have a shift planned yet" -f $dayDate, $email)
#A pokud není v seznamu těch, co směnu mít dnes nemají...
if ($email -notin $ns_emails) {
Write-Debug("{0}: {1} is not on a list of no-shift emails for today, setting office shift" -f $dayDate, $email)
#Nastavíme office směnu
$newshift = Set-Shift -userId $email_userId -groupID $group.id -shiftName "Office" -StartDate $dateStart -EndDate $dateEnd -color "blue" -teamID $team.id
if($email -in $ho_emails) {
if((Invoke-HasShiftorTimeOff -UID $userId -shifts $shifts_today -timeoff $timeoff_today -mail $email) -eq $false) {
$newshift = Set-Shift -userId $userId -groupID $group.id -shiftName "Home office" -StartDate $dateStart -EndDate $dateEnd -color "pink" -teamID $team.id -mail $email
$allshifts += [Object[]] $newshift
}
else {
Write-Debug("{0}: {1} isn't supposed to have a shift today, not setting a shift" -f $dayDate, $email)
}
}
else {
Write-Debug("{0}: {1} has a shift planned already, can't set office shift" -f $dayDate, $email)
}
}
continue
}
}
$params = @{
notifyTeam = $false
startDateTime = [System.DateTime]::Parse($startSpanDate.ToString("yyyy-MM-dd'T'HH:mm:ssZ"))
endDateTime = [System.DateTime]::Parse($endSpanDate.ToString("yyyy-MM-dd'T'HH:mm:ssZ"))
if((Invoke-HasShiftorTimeOff -UID $userId -shifts $shifts_today -timeoff $timeoff_today -mail $email) -eq $false) {
$newshift = Set-Shift -userId $userId -groupID $group.id -shiftName "Office" -StartDate $dateStart -EndDate $dateEnd -color "blue" -teamID $team.id -mail $email
$allshifts += [Object[]] $newshift
continue
}
}
Invoke-MgShareTeamSchedule -TeamId $team.Id -BodyParameter $params -Headers @{ "MS-APP-ACTS-AS" = $userIdAdmin }
Write-Debug ("SHARING: {0}" -f $scheduleGroupName)
("SHARING: {0}" -f $scheduleGroupName) | Add-Content -Path $logPath
}
}
#$params = @{
# notifyTeam = $false
# startDateTime = [System.DateTime]::Parse($startSpanDate.ToString("yyyy-MM-dd'T'HH:mm:ssZ"))
# endDateTime = [System.DateTime]::Parse($endSpanDate.ToString("yyyy-MM-dd'T'HH:mm:ssZ"))
#}
#Invoke-MgShareTeamSchedule -TeamId $team.Id -BodyParameter $params -Headers @{ "MS-APP-ACTS-AS" = $userIdAdmin }
#("SHARING: {0}" -f $scheduleGroupName) | Add-Content -Path $logPath
}
catch {