Search code examples
powershellkubernetesexeckubectlpowershell-ise

kubectl exec command not working from powershell ISE (works from powershell) - Is there a reason or workaround to make it work?


I have recently started working with Kubernetes and Docker and still new with how it all works. I have made a ps1 script to run all the steps I need to do to build image and execute image on Kubernetes.

What I see is all steps work fine on ISE (except this one: "kubectl exec -it test-runner pwsh"). For this step alone, I have to run it on another PowerShell window.

When I run this step in ISE, the script keeps running without ever giving any error or stopping.

Does anyone know if its a limitation with Kubernetes working on ISE or if there is a workaround to make it work?

Working with ISE is quick and saves me tons of time, so this really makes a difference when I have to copy, paste, enter this each time in a separate PowerShell window.

Thanks in advance for your help!

P.S: I looked at other suggested similar questions/answers and none of them seem to be related to Kubernetes not working on ISE. Hence this question.

command:

kubectl exec -it test-runner pwsh

Expected (and actual when running from PowerShell console):

----------------------
PS C:\windows\system32> kubectl exec -it test-runner pwsh
PowerShell 6.2.2
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS /test>
-----------------------------
Actual (when running from PowerShell ISE):

PS C:\SourceCodeTLM\Apollo>  kubectl exec -it test-runner pwsh
PowerShell 6.2.2
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

(with a blinking cursor and script running without breaking and changing to the new path)...
-----------------------------------------------

Solution

  • The PowerShell ISE doesn't support interactive console applications, which notably means that you cannot start other shells from it.

    The ISE tries to anticipate that problem by refusing to start well-known shells. For instance, trying to start cmd.exe fails with the following error message:

    Cannot start "cmd". Interactive console applications are not supported. 
    To run the application, use the Start-Process cmdlet or use 
    "Start PowerShell.exe" from the File menu.
    

    Note:

    However, it is impossible for the ISE to detect all cases where a given command (ultimately) invokes an interactive console application; when it doesn't, invocation of the command is attempted, resulting in obscure error messages or, as in your case, hangs.

    As the error message shown for cmd.exe implies, you must run interactive console applications outside the ISE, in a regular console window.

    From the ISE you can use Start-Process to launch a program in a new, regular console window; in the case at hand:

    Start-Process kubectl 'exec -it test-runner pwsh'
    

    Alternatively, run your PowerShell sessions outside the ISE to begin with, such as in a regular console window, Windows Terminal, or in Visual Studio Code's integrated terminal.