r/PowerShell • u/baron--greenback • Nov 01 '23
Script Sharing TimeKeeping Assistant
Hi All,
Unexpectedly received some interest when posting my 'what have you used Powershell for this month' and have been asked to share - below is the script I mashed together to improve my logging of how I spend my time at work.
It's a simple 'new calendar event' command wrapped in a simple GUI prompt.
An intentionally obnoxious winform pops up asking how I spent most of the last hour. I made it as minimal as possible because I want to complete it without interrupting whatever I'm working on. There are two input fields - selecting a category using a dropdown Combo-Box and a Textbox for adding details The category forms the name of the calendar event and I have matching categories setup in Outlook which colour codes the events, The textbox details form the body of the calendar event.
Here are some screenshots - https://imgur.com/a/VJkZgDk
I have a scheduled task to run the script every hour and a second weekly script which counts how many hours I spent in the previous week on each category and sends me an email.
This script uses an app registration to connect to Graph and needs Calendars.ReadWrite permissions.
This was originally just for me and not intended to look nice so please be gentle with your replies. Happy for others to steal and suggest improvements :)
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Connect to Graph
Import-Module -name Microsoft.Graph.Beta.Calendar
Connect-MgGraph -ClientID "__" -TenantId "__" -CertificateThumbprint "__" | out-null
# UserID and CalendarID
$user = "__"
$userid = (get-mguser -userid "$user").id
$calid = (get-mgusercalendar -userid "$user" | where-object { $_.Name -eq 'Calendar' }).id
# Messy way to calculate date and put into the correct format
$Date = get-date -Format yyyy-MM-dd
$Time = get-date -Format HH:00:00
$starthourdiscovery = (get-date -format HH ) - 1
if ( ($starthourdiscovery | Measure-Object -Character).Characters -lt '2' ){ $starthour = "0$starthourdiscovery" }
else { $starthour = "$starthourdiscovery" }
$starttime = (get-date -Format $starthour+00:00).Replace("+",":")
$fullstarttime = $date + "T" + $starttime
$fullendtime = $date + "T" + $Time
# Create a new form
$CompanionWindow = New-Object system.Windows.Forms.Form
$CompanionWindow.startposition = 'centerscreen'
$CompanionWindow.TopMost = $true
# Define the size, title and background
$CompanionWindow.ClientSize = '500,100'
$CompanionWindow.MaximumSize = $CompanionWindow.Size
$CompanionWindow.MinimumSize = $CompanionWindow.Size
$CompanionWindow.text = "Calendar Companion: $starttime - $time"
$CompanionWindow.FormBorderStyle = "FixedSingle"
$CompanionWindow.BackColor = "Chocolate"
$Font = New-Object System.Drawing.Font("Ariel",13)
# Text Input
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(32,60)
$textBox.Size = New-Object System.Drawing.Size(440,30)
$textBox.Height = 20
$textBox.BackColor = "DarkGray"
$textBox.ForeColor = "Black"
$textBox.BorderStyle = "None"
$textBox.Font = $font
$textBox.TabIndex = 1
$CompanionWindow.Controls.Add($textBox)
# Sits under textbox to give a small border
$header = New-Object System.Windows.Forms.label
$header.Location = New-Object System.Drawing.Point(26,57)
$header.Height = 29
$header.Width = 450
$header.BackColor = "DarkGray"
$header.BorderStyle = "FixedSingle"
$CompanionWindow.Controls.Add($header)
# Categories Dropdown
# Possible to auto-extract these from Outlook?
$CategoryList = @(
'BAU'
'Documentation'
'Escalation'
'Lunch'
'Ordering'
'Project'
'Reactionary'
'Reading'
'Routine Tasks'
'Scripting'
'Training ( Providing )'
'Training ( Receiving )'
)
$Categories = New-Object system.Windows.Forms.ComboBox
$Categories.location = New-Object System.Drawing.Point(27,18)
$Categories.Width = 340
$Categories.Height = 30
$CategoryList | ForEach-Object {[void] $Categories.Items.Add($_)}
$Categories.SelectedIndex = 0
$Categories.BackColor = "DarkGray"
$Categories.ForeColor = "Black"
$Categories.FlatStyle = "Flat"
$Categories.Font = $Font
$Categories.MaxDropDownItems = 20
$Categories.TabIndex = 0
$CompanionWindow.Controls.Add($Categories)
#Submit Button
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(375,17)
$Button.Size = new-object System.Drawing.Size(100,30)
$Button.Text = "Submit"
$Button.BackColor = "DarkGray"
$Button.ForeColor = "Black"
$Button.FlatStyle = "Flat"
$Button.Add_Click({
$params = @{
subject = $Categories.SelectedItem
Categories = $Categories.SelectedItem
body = @{
contentType = "HTML"
content = $textBox.Text
}
start = @{
dateTime = "$fullstarttime"
timeZone = "GMT Standard Time"
}
end = @{
dateTime = "$fullendtime"
timeZone = "GMT Standard Time"
}
}
New-MgBetaUserCalendarEvent -UserId $userid -CalendarId $calid -BodyParameter $params | Out-Null
[void]$CompanionWindow.Close()
})
$CompanionWindow.Controls.Add($Button)
# Display the form
$CompanionWindow.AcceptButton = $button
[void]$CompanionWindow.ShowDialog()
4
u/chrusic Nov 01 '23 edited Jun 03 '26
¯_(ツ)_/¯ (Can't belive reddit is making me do this manually...)
4
u/Tie_Pitiful Nov 02 '23
Thanks, I was one of the ones who asked you for this. Well done and thank you :)
4
u/anon_goes_reddit Nov 02 '23
Thank you for sharing mate! More concise script than I expected. Goes to show my own, similar script is quite overengineered & bloated ^^
3
u/FIREPOWER_SFV Nov 02 '23
Very cool!, I did something similar but nowhere near as nice, i'll be giving this a try, thanks.
3
u/surfingoldelephant Nov 02 '23 edited Nov 17 '23
Thanks for sharing!
This can be simplified to:
Then to output in your desired format:
With this approach, you retain the initial
[datetime]objects instead of immediately converting them to strings. This allows them to be formatted as many times as needed without having to instantiate new[datetime]objects. It also fixes a bug in your code that mishandles midnight (though hopefully this isn't an applicable use case! ;-)).New-Objectis slow, so you may want to consider usingnew()or typecasting with a hashtable (shown below) to instantiate your form objects. Ausing namespacestatement can also be placed at the top of the script to improve readability.For example:
2
u/whdescent Nov 02 '23
Seems like a neat tool. From a calendaring perspective, my suggestion would be to incorporate the $ShowAs parameter into either the form or based on certain categories.
As it stands right now, your Outlook calendar would show every time windows as BUSY. Not that there is anything wrong with that if it matches your workplace conditions. That said, who of us can really avoid meetings, for which accurate FREE/BUSY information is key to effective collaboration and scheduling?
2
u/[deleted] Nov 03 '23
Ohh! That‘s really clever! Love it!
2
u/ReindeerUnlikely9033 Nov 17 '23
I really really like the form integration I have not done that with Powershell. Most of my scripts are used in pipelines so no user input and it's cool to see what you can do in this area. Thanks for sharing. Gave me a lot of food for thought.
1
u/l0wet Nov 15 '23
Awesome, thanks for sharing!
Does anyone know if all users will get notified if I grant the azure app calendar.read/write permission on behalf of my tenant?
Also, do you mind sharing the weekly script you run to sum up the time spent on each category? :)
1
u/[deleted] Nov 02 '23
[removed] — view removed comment
0
u/[deleted] Nov 02 '23
[removed] — view removed comment