powershell

PowerShell Multiple Catch Issue


I'm following Microsoft examples and I get an error when I run this in PowerShell (5.1). I am unable to repro this in the ISE.

$app="notepad2354.exe"
try{
   iex $app
}
catch [System.Management.Automation.CommandNotFoundException]{
   write-host 'ERROR! The app file could not be found.'
}
catch {
   write-host 'ERROR! Unknown error when executing the step. Error: ' + $_.Exception.Message
}

Error:

catch : The term 'catch' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

The odd thing is it works fine on the first catch. If I switch the order, the second one always fails with this message. Any ideas why this is error'ing?


Solution

  • To add to @UnhandledExcepSean answer where he saw that his issue is with Copy\Pasting into PowerShell console.

    The root issue is that PSReadline changed the right-click pasting functionality.

    PSReadline, originally offered as an installable module for PowerShell v3 and v4, now comes default with PowerShell v5+ and PowerShell Core 6+. It is a nice module that adds a bunch of new things like syntax highlighting, better multi-line editing experience, etc.

    When PSReadline is imported, Right-Click pasting doesn't "work" as expected see GitHub issue: Right click paste should work mostly like Ctrl+v paste 579. What happens is in:

    PSReadLine, if the input is "complete", as in, it parses without an IncompleteParseException, then the input will be accepted...

    [https://github.com/PowerShell/PSReadLine/issues/579#issuecomment-345824783]

    Basically, the Right-Click pasting comes from the Windows PowerShell terminal "GUI" host, and streams in the characters into the PowerShell console, where they are being intercepted and interpreted by PSReadline to provide things like syntax highlighting. As it streams in, the trailing } curly brace + newline, completes the statement, and it executes. The second catch statement on the new line then errors out because it is, indeed, on it's own not correct.

    Instead, if you use Ctrl+V to paste, the above Copy/Pasting of code does work. the Ctrl+V functionality delivers the entire clipboard contents at once to PSReadline, which interprets everything as a whole before continuing with the execution.

    You don't see this happen PowerShell ISE or scripts because Right-Click Pasting doesn't happen.

    For fun, you can remove PSReadline in your PowerShell console session:

    Remove-Module PSReadline
    

    And then see that Right-Click paste now works as "expected".... Although all the goodness of PSReadline are also gone :-(

    Since the Right-Click functionality is specifically a Windows PowerShell terminal issue, even if PSReadline wanted to intercept the Right-Click mouse-hook, it would require PSReadline to now use Windows specific API's to emulate the Windows clipboard feature. This go against "Core" cross-platform functionality. This means that this Right-Click anomaly will likely persist with Ctrl+V to paste as the only recommended way to paste.