Creating an On Call Calendar Rotation with Powershell

Our company was using a Sharepoint Calendar for all of the on call stuff. Which was very hard to manage especially if we got a new employee, swap on call weeks, etc. So I came up with an idea to create a webpage as well as sending an email every Tuesday (Our swap day) to notify everyone who is on call. It also provided the accurate dates on when you would be on call.

Above is the sample HTML file created by my script. I created a scheduled task to run this script nightly to keep the remote days updated. It also will rotate the user when Tuesday comes around to the bottom. You can change it to what ever you need. To run this, just create a CSV with the headers “Name”,”Email”,”Cell”,”Area” in the folder you are running the Powershell Script. If you have any problems just let me know!

[int]$DateCounter = 0 [int]$StartDateCounter = 0 [int]$CycleCount = 0 [String]$DayofWeek = (get-date).DayOfWeek [Switch]$SendMail = $False [String]$Runtime = get-date -Format g Set-Location $PSScriptRoot If ($DayOfWeek -ne "Tuesday") { $DoNotRotate = $True } $Website = @() $NewList = @() $Website += "<html>" $Website += "<head>" $Website += " <title>IT Engineering Schedule</title>" $Website += "<style>" $Website += "body {" $Website += " color: #666;" $Website += ' font-family: /*"Open Sans", */Arial, Helvetica, sans-serif;' $Website += " line-height: 1.5;" $Website += " font-size: 16px;" $Website += "}" $Website += ".container {" $Website += " max-width: 800px;" $Website += " width: 80%;" $Website += " margin: 50px auto 0;" $Website += "}" $Website += "" $Website += "a {" $Website += " color: #0053a0;" $Website += " text-decoration: none;" $Website += "" $Website += "}" $Website += ".notice {" $Website += " display: block;" $Website += " font-weight: bold;" $Website += " color: rgb(177, 39, 39);" $Website += " border: 1px solid #ccc;" $Website += " border-radius: 2px;" $Website += " padding: .5em 5%;" $Website += " background-color: #f1f1f1;" $Website += " text-align: center;" $Website += " font-size: 1.2em;" $Website += "}" $Website += ".calendar {" $Website += " display:block;" $Website += " text-align: left;" $Website += " font-size: 1.5em;" $Website += " font-weight: bold;" $Website += " color: black;" $Website += " background-color: #FFFFFFF;" $Website += " -webkit-transition: background-color .1s ease-in-out;" $Website += " -moz-transition: background-color .1s ease-in-out;" $Website += " -o-transition: background-color .1s ease-in-out;" $Website += " transition: background-color .1s ease-in-out;" $Website += "}" $Website += ".calendar:hover {" $Website += " color: white;" $Website += " background-color: #6F94B6;" $Website += " cursor: pointer;" $Website += " -webkit-transition: background-color .1s ease-in-out;" $Website += " -moz-transition: background-color .1s ease-in-out;" $Website += " -o-transition: background-color .1s ease-in-out;" $Website += " transition: background-color .1s ease-in-out;" $Website += "}" $Website += ".calendar:active {" $Website += " background-color: #A7BED2;" $Website += " color: white;" $Website += "}" $Website += "" $Website += "p, h3 span {" $Website += " color: #989898;" $Website += " font-size: 80%;" $Website += "}" $Website += "" $Website += "@media all and (max-width: 600px) and (min-width: 0px) {" $Website += " body {" $Website += " font-size: 14px;" $Website += " }" $Website += " .container {" $Website += " margin: 0 auto;" $Website += " width: 95%;" $Website += " }" $Website += " .notice {" $Website += " padding: .5em .5em;" $Website += " }" $Website += " .calendar {" $Website += " font-size: 1em;" $Website += " padding: 2%;" $Website += " }" $Website += "}" $Website += "" $Website += "li { background:white; }" $Website += "li.odd { background: #f1f1f1; }" $Website += "table {" $Website += " width:100%;" $Website += "}" $Website += "table, th, td {" $Website += " border-collapse: collapse;" $Website += "}" $Website += "th, td {" $Website += " padding: 1px;" $Website += " text-align: left;" $Website += "}" $Website += "table#t01 tr:nth-child(even)" $Website += "{" $Website += " background-color: #eee;" $Website += "}" $Website += "table#t01 tr:nth-child(odd)" $Website += "{" $Website += " background-color: #fff;" $Website += "}" $Website += "table#t01 th {" $Website += ' background-color: #989898;' $Website += " color: white;" $Website += "}" $Website += "</style>" $Website += "</head>" $Website += "<body>" $Website += '<div class="container">' $Website += '<p class="notice">IT Engineering Schedule. If you are Highlighted you are On Call :)</p>' $Website += "" $Website += "<h3>On Call List</h3>" $Website += "<table>" If ($DonotRotate) { $NewList = Import-Csv ".\OnCallList.csv" } Else { #Check if Already Rotated this Tuesday [string]$TuesdayDate = get-date -Format MM.dd.y $TuesdayCheck = Get-Content ".\rotatecheck.txt" $CheckTuesday = $TuesdayCheck | Select-String $tuesdaydate If ($CheckTuesday) { $NewList = Import-Csv ".\OnCallList.csv" } Else { #Append Date to File Out-File -FilePath ".\rotatecheck.txt" -Append -InputObject $TuesdayDate #Gather List of users $ListofUsers = Import-Csv ".\OnCallList.csv" foreach ($User in $ListofUsers) { #Move Top of the list to bottom If ($User -eq $ListofUsers[0]) { #Skip the user } Else { $NewList += $User } } #Add the First user to bottom $NewList += $ListofUsers[0] $NewList | Export-Csv -path ".\OnCallList.csv" -NoTypeInformation -Force $SendMail = $True $OnCallUser = $NewList[0].Name $Subject = "$OnCallUser is on call this week" $OnCallEmail = $NewList[0].Email } } Foreach ($OnCall in $NewList) { $Website += "<tr>" if ($DonotRotate -and $CycleCount -eq 0) { $i = 0 do { $LastTuesday = (Get-Date).AddDays(--$i).Date } until ($LastTuesday.DayofWeek -eq 'Tuesday') $StartDate = $LastTuesday.ToString('MM-dd-yyyy') $Plus6Days = $LastTuesday.AddDays(6).ToString('MM-dd-yyyy') $Website += '<td> <font color="Red">' + "$($OnCall.Name)" + "</td>" $Website += '<td> <font color="Red">' + "$($OnCall.Cell)" + "</td>" $Website += '<td> <font color="Red">' + "$($OnCall.Area)" + "</td>" $Website += '<td> <font color="Red">' + "$StartDate - $Plus6Days" + "</td></font>" [int]$Math = 7 + $i $StartDateCounter = $StartDateCounter + $Math } Else { $DateCounter = $StartDateCounter + 6 $StartDate = (Get-Date).AddDays($StartDateCounter).ToString('MM-dd-yyyy') $Plus6Days = (Get-Date).AddDays($DateCounter).ToString('MM-dd-yyyy') If ($CycleCount -eq 0) { $Website += '<td> <font color="Red">' + "$($OnCall.Name)" + "</td>" $Website += '<td> <font color="Red">' + "$($OnCall.Cell)" + "</td>" $Website += '<td> <font color="Red">' + "$($OnCall.Area)" + "</td>" $Website += '<td> <font color="Red">' + "$StartDate - $Plus6Days" + "</td></font>" } Else { $Website += "<td> $($OnCall.Name) </td>" $Website += "<td> $($OnCall.Cell) </td>" $Website += "<td> $($OnCall.Area) </td>" $Website += "<td> $StartDate - $Plus6Days </td>" } $StartDateCounter = $StartDateCounter + 7 } $Website += "</tr>" $CycleCount++ } $Website += "" $Website += " </ul>" $Website += " </table>" $Website2 = $Website $Website2 += "" $Website2 += "<h3>Who's Remote Today?</h3>" $Website2 += " <ul>" $Website2 += "" Switch ($DayofWeek) { "Tuesday" { $Website2 += "<li>Joe Shmoe</li>" $Website2 += "<li>Jon Smith</li>" $Website2 += "<li>Michael Jackson</li>" } "Wednesday" { $Website2 += "<li>R Kelly</li>" $Website2 += "<li>Alejandro Perez</li>" $Website2 += "<li>Anthony P</li>" $Website2 += "<li>Lewis Clarke</li>" $Website2 += "<li>Larry Bird</li>" $Website2 += "<li>Michael Jordan</li" } "Thursday" { $Website2 += "<li>Ken Streetfighter</li>" $Website2 += "<li>Cool Guy</li>" $Website2 += "<li>Funny Alvarez</li>" } "Friday" { $Website2 += "<li>R Kelly</li>" $Website2 += "<li>Alejandro Perez</li>" $Website2 += "<li>Anthony P</li>" } default { $Website2 += "<li> None </li>" } } $Website2 += "" $Website2 += " </ul>" $Website2 += "<p><span>Note: The List rotates every Tuesday Morning. If you need to swap with anyone ask a person close to your rotation.<br>" $Website2 += "An Email is Generated every Tuesday Morning. Script was Last Ran at $RunTime </span></p>" $Website2 += "" $Website2 += "" $Website2 += "<br>" $Website2 += "</body>" $Website2 += "</html>" $Website += "<p><span>Note: The List rotates every Tuesday Morning. If you need to swap with anyone ask a person close to your rotation.<br>" $Website += "An Email is Generated every Tuesday Morning. Script was Last Ran at $RunTime </span></p>" $Website += "" $Website += "" $Website += "<br>" $Website += "</body>" $Website += "</html>" $website2 | Out-File ".\index.html" -Force if ($SendMail) { $Body = $website | Out-String Send-MailMessage -From ITEngineeringoncall@contoso.com -To "itengineering@contoso.com" -BodyAsHtml -Body $Body -SmtpServer smtp.wellstar.org -Subject $Subject }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s