Skip to content

Instantly share code, notes, and snippets.

@devblackops devblackops/redalert.ps1
Last active Jul 1, 2019

Embed
What would you like to do?
Display highly visible notification if the last command failed in the Microsoft Terminal using PowerShell
# 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
}
}
}
@gpduck

This comment has been minimized.

Copy link

commented Jun 29, 2019

Great idea, this is fun :)

Set-MSTerminalProfile -Name pwsh -CommandLine "pwsh -noexit -command `$env:WT_PROFILE = 'pwsh'"
Set-MSTerminalProfile -Name powershell -CommandLine "powershell -noexit -command `$env:WT_PROFILE = 'powershell'"

function prompt {
  $IsFail = !$?
  if(!$Global:__PromptPS) {
    $Runspace = [runspacefactory]::CreateRunspace()
    $Global:__PromptPS = [PowerShell]::Create()
    $Global:__PromptPS.Runspace = $Runspace
    $Runspace.Open()
    [void]$Global:__PromptPS.Addscript({
      ipmo msterminal
      $OldProfile = Get-MSTerminalProfile -Name $env:WT_PROFILE
      Set-MSTerminalProfile -name $env:WT_PROFILE -BackgroundImage 'https://media.giphy.com/media/ErE2BHnoCWsYU/giphy.gif' -backgroundImageStretchMode uniformToFill -UseAcrylic:$False
      Start-Sleep -Seconds 2.41
      $OldProfile | Set-MSTerminalProfile
    })
  }
  if($IsFail) {
    [void]$Global:__PromptPS.BeginInvoke()
  }
  "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
}
# Use this to reset the runspace if you update the function
$global:__PromptPS.Dispose(); $global:__PromptPS = $null
@masample-ms

This comment has been minimized.

Copy link

commented Jul 1, 2019

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();
}
@paulcam206

This comment has been minimized.

Copy link

commented Jul 1, 2019

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
You can’t perform that action at this time.