Last active
August 15, 2023 08:02
-
-
Save devblackops/2dea8440b48b4d378f220841c07ec2a2 to your computer and use it in GitHub Desktop.
Display highly visible notification if the last command failed in the Microsoft Terminal using PowerShell
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
# Put this code in your PowerShell profile script | |
# This requires the MSTerminalSettings module which you can download with: | |
# Install-Module MSTerminalSettings -Scope CurrentUser -Repository PSGallery | |
Import-Module MSTerminalSettings | |
$msTermProfileName = 'pwsh' # Replace with whatever Terminal profile name you're using | |
$msTermProfile = Get-MSTerminalProfile -Name $msTermProfileName | |
$script:bombThrown = $false | |
function prompt { | |
if ($? -eq $false) { | |
# RED ALERT!!! | |
# Only do this if we're using Microsoft Terminal | |
if ((Get-Process -Id $PID).Parent.Parent.ProcessName -eq 'WindowsTerminal') { | |
Set-MSTerminalProfile -Name $msTermProfile.name -BackgroundImage 'https://media.giphy.com/media/HhTXt43pk1I1W/giphy.gif' -UseAcrylic:$false | |
$script:bombThrown = $true | |
} | |
} else { | |
# Reset to previous settings | |
if ($script:bombThrown) { | |
Set-MSTerminalProfile -Name $msTermProfile.name -BackgroundImage $msTermProfile.backgroundImage -UseAcrylic:$msTermProfile.useAcrylic | |
$script:bombThrown = $false | |
} | |
} | |
} |
For bonus notification visibility, you could add this function and point it at an appropriate sound when you error.
Function Play-Sound{
Param(
[Parameter(Mandatory=$True,Position=0)]$SoundFile
)
$player = New-Object System.Media.SoundPlayer $SoundFile
$player.Play();
}
You might consider adding a blowoff valve for robocopy
:
The return code from Robocopy is a bitmap, defined as follows:
Hex Decimal Meaning if set
0×00 0 No errors occurred, and no copying was done.
The source and destination directory trees are completely synchronized.
0×01 1 One or more files were copied successfully (that is, new files have arrived).
0×02 2 Some Extra files or directories were detected. No files were copied
Examine the output log for details.
0×04 4 Some Mismatched files or directories were detected.
Examine the output log. Housekeeping might be required.
0×08 8 Some files or directories could not be copied
(copy errors occurred and the retry limit was exceeded).
Check these errors further.
0×10 16 Serious error. Robocopy did not copy any files.
Either a usage error or an error due to insufficient access privileges
on the source or destination directories.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great idea, this is fun :)