Last active
June 15, 2021 06:39
-
-
Save Hashbrown777/f41554e32b3e7a6a8023a6767a9a4798 to your computer and use it in GitHub Desktop.
Gets all your unread emails from all your accounts and subdirectories in outlook and presents them to you in a table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. ./async.ps1 | |
. ./asynclet.ps1 | |
. ./unicode.ps1 | |
. ./windows.ps1 | |
. ./wrap.ps1 | |
$_TABLE_TITLE = 'The powershell window will show progress. You can email or run again by making a selection' | |
$timer = [system.diagnostics.stopwatch]::StartNew() | |
$selection = [Asynclet]::new( | |
'Out-GridView', | |
@{PassThru=$True; Title=$_TABLE_TITLE} | |
) | |
$now = Get-Date | |
$outlook = New-Object -ComObject 'Outlook.Application' | |
$asyncId = Get-Random | |
Write-Progress -Id $asyncId -Activity 'Getting folders' | |
$emails = $outlook.GetNamespace('mapi') ` | |
| %{ | |
$total = 0 | |
$complete = 0 | |
For ( | |
$folders = [System.Collections.Stack]@($_.Folders); | |
$folders.Count; | |
) { | |
$folder = $folders.Pop() | |
++$total | |
$location = $folder.FolderPath | |
$path = $location -replace '^\\\\|@[^\\]+(?=\\)','' -replace '\\',"`t" | |
Switch -Regex ($path) { | |
'^<<REDACTED>>(\t[^\t]+)*\t(_read|archive)$' {} | |
'^<<REDACTED>>\tInbox\t(JIRA|Peeps\t<<REDACTED>>)$' {} | |
'^([^\t]+)\t(Calendar|Files|Outbox|Contacts|Junk Email|Sync Issues|(Sent|Deleted) Items|Archive)$' {} | |
Default { | |
$total -= $folders.Count | |
$folder.Folders ` | |
| %{ $folders.Push($_) } | |
$total += $folders.Count | |
,(@($path, $location),$folder.Items) | |
Write-Progress ` | |
-Id $asyncId ` | |
-Activity 'Getting folders' ` | |
-Status $path ` | |
-PercentComplete (100 * ++$complete / $total) | |
} | |
} | |
} | |
Write-Progress -Id $asyncId -Activity 'Getting folders' -Completed | |
} ` | |
| Async ` | |
-Name 'Processing Emails' ` | |
-BatchSize 5 ` | |
-Parameters @{ | |
til = $now.Add([timespan]::new(1, 0, 0, 0)) | |
from = $now.Subtract([timespan]::new(28, 0, 0, 0)) | |
now = $now | |
} ` | |
-Func { | |
Param($til, $from, $now) | |
$path = $NULL | |
$location = $NULL | |
$Input ` | |
| %{ | |
if ($path) { | |
$_ | |
} | |
else { | |
$path = $_[0] | |
$location = $_[1] | |
} | |
} ` | |
| ?{ | |
$_.Unread -and | |
$_.SentOn -gt $from -and | |
$_.SentOn -lt $til | |
} ` | |
| %{ | |
$item = $path | |
if ($item -notmatch '^<<REDACTED>>\tInbox\tPeeps') { | |
$email = $_.SenderEmailAddress() | |
$item += "`t" + ($email,$_.SenderName())[ | |
$_.SenderEmailType() -eq 'EX' -or | |
$email -match '@pco.nsw.gov.au$' | |
] | |
} | |
<#[PSCustomObject]#>@{ | |
Item = $item | |
Subject = $_.Subject | |
Ago = $now - $_.SentOn | |
Sent = $_.SentOn | |
_ = $_ | |
} | |
} | |
} ` | |
| ?{ $_ } ` | |
| %{ #around June 2021 pwsh runtimes stopped being able to return PSCustomObject's for some reason, rebuild it from the returnable hashtable above | |
[PSCustomObject]@{ | |
Item = $_['Item'] | |
Subject = $_['Subject'] | |
Ago = $_['Ago'] | |
Sent = $_['Sent'] | |
_ = $_['_'] | |
} | |
} ` | |
| %{ | |
$_.Item = ( | |
$_.Item ` | |
-replace '(?=\+61)',(0x1F4DE | Unicode) ` | |
-creplace '(?<=PCO\t)(?=All)',(0x1F4E2 | Unicode) ` | |
-creplace '\tInbox(?=\t)','' ` | |
-creplace '^<<REDACTED>>\t','' ` | |
-replace '(?<=\t)(?!= )',' ' | |
) | |
$_.Subject = Wrap ( | |
0x1F4E9,' ',($_.Subject -replace '^((re|fwd?): *)+','') ` | |
| Unicode | |
) | |
$_.Ago = (.{ | |
Switch ($_.Ago) { | |
{ $_.TotalMinutes -lt 90 } { [Math]::Ceiling($_.TotalMinutes),'min' ; break } | |
{ $_.TotalHours -lt 24 } { [Math]::Ceiling($_.TotalHours ),'hrs' ; break } | |
{ $_.TotalDays -lt 14 } { [Math]::Ceiling($_.TotalDays ),'days'; break } | |
Default { | |
[Math]::Ceiling($_.TotalDays / 7),'wks' | |
} | |
} | |
}) -join '' | |
$_.Sent = $_.Sent.ToString(( | |
( | |
'MM/dd ', | |
0x2014, | |
' HH:mm ', | |
(0x1F312,0x26C5)[!( | |
$_.Sent.Hour -lt 9 -or $_.Sent.Hour -gt 18 | |
)] | |
) ` | |
| Unicode | |
)) | |
$selection.input($_) | |
$_ | |
} | |
#main loop | |
For ($continue = $True; $continue;) { | |
if (!$selection) { | |
$timer = [system.diagnostics.stopwatch]::StartNew() | |
$selection = [Asynclet]::new( | |
'Out-GridView', | |
@{PassThru=$True; Title=$_TABLE_TITLE} | |
) | |
$emails ` | |
| %{ | |
$selection.input($_) | |
} | |
} | |
(" | |
Completed in | |
$([Math]::Floor($timer.Elapsed.TotalMinutes)) | |
: | |
$($timer.Elapsed.Seconds -replace '^(.)$','0$1') | |
" -replace '\r?\n\t*','') | |
Title 'You may now make a selection and close the table' | |
$_TITLE | |
BringToFront $_TABLE_TITLE | |
$selection=` | |
try { | |
,$selection.done() | |
} | |
catch { | |
@() | |
} | |
$NULL | ResetScreen | |
BringToFront $_TITLE | |
$remove = @{} | |
$selection ` | |
| %{ | |
$remove[($_.Item + $_.Sent) -replace '[^\x00-\xFF]',''] = $True | |
} | |
$emails = $emails ` | |
| ?{ !$remove[($_.Item + $_.Sent) -replace '[^\x00-\xFF]',''] } | |
Title "$($selection.Count) of $($emails.Count + $selection.Count) unread emails selected" | |
#finish up with a total time and wait for the user to close | |
Switch -exact ($Host.UI.PromptForChoice( | |
$_TITLE, | |
'What would you like to do now?', | |
('E&xit', 'Go &back', '&Open', '&Filter', '&Reverse selection'), | |
#make sure exit is selected | |
1 | |
)) { | |
0 { | |
$continue = $False | |
} | |
1 { | |
$emails += $selection | |
} | |
2 { | |
$selection ` | |
| %{ | |
$_._.Display() | |
} | |
} | |
4 { | |
$emails = $selection | |
} | |
} | |
$selection = $NULL | |
} | |
'Good work ',0x1F44D | Unicode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment