【问题标题】:kubectl exec command not working from powershell ISE (works from powershell) - Is there a reason or workaround to make it work?kubectl exec 命令不能从 powershell ISE 工作(从 powershell 工作) - 是否有理由或解决方法使其工作?
【发布时间】:2019-12-28 13:15:49
【问题描述】:

我最近开始使用 Kubernetes 和 Docker,但对它们的工作原理还是很陌生。我制作了一个 ps1 脚本来运行在 Kubernetes 上构建映像和执行映像所需的所有步骤。

我看到的是所有步骤在 ISE 上都可以正常工作(除了这个:“kubectl exec -it test-runner pwsh”)。仅对于这一步,我必须在另一个 PowerShell 窗口上运行它。

当我在 ISE 中运行此步骤时,脚本会继续运行,不会出现任何错误或停止。

有谁知道 Kubernetes 在 ISE 上工作是否存在限制,或者是否有解决方法可以使其工作?

使用 ISE 速度很快,并且为我节省了大量时间,因此当我每次都必须在单独的 PowerShell 窗口中复制、粘贴和输入时,这真的很重要。

提前感谢您的帮助!

P.S:我查看了其他建议的类似问题/答案,但似乎没有一个与 Kubernetes 无法在 ISE 上运行有关。因此这个问题。

命令:

kubectl exec -it test-runner pwsh

预期的(以及从 PowerShell 控制台运行时的实际情况):

----------------------
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)...
-----------------------------------------------

【问题讨论】:

    标签: powershell kubernetes exec kubectl powershell-ise


    【解决方案1】:

    PowerShell ISE 不支持交互式控制台应用程序,这意味着您无法启动其他shell 来自它。

    ISE 试图通过拒绝启动众所周知的 shell 来预测该问题。 例如,尝试启动 cmd.exe 失败并显示以下错误消息:

    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.
    

    注意:

    但是,ISE 不可能检测到给定命令(最终)调用交互式控制台应用程序的所有情况;如果没有,则尝试调用该命令,从而导致模糊的错误消息,或者在您的情况下挂起。

    正如错误消息所暗示的,您必须在 ISE 之外在常规控制台窗口中运行交互式控制台应用程序。

    从 ISE,您可以使用 Start-Process 在新的常规控制台窗口中启动程序;在手头的情况下:

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

    或者,首先在 ISE 外部运行 PowerShell 会话,例如在常规控制台窗口、Windows 终端或 Visual Studio Code 的集成终端中。

    【讨论】:

      【解决方案2】:

      在挖掘之后,我找到了一种与交互式控制台应用程序交互的方法。

      我们可以将SendKeys('key_here') 用于特定的应用程序。

      【讨论】:

      • 请注意,这个问题是关于根本无法在 ISE 中运行交互式控制台应用程序。您正在回答一个不同的问题,即如何自动与交互式控制台应用程序交互(最好通过管道/标准输入而不是通过模拟用户操作来完成,后者总是很脆弱)。
      • 哦,好吧,我误会了,感谢您的澄清。
      【解决方案3】:

      您可以从 PowerShell 打开命令窗口并发送命令以打开 shell。

      Start-Process PowerShell -Wait "-NoProfile -ExecutionPolicy Bypass -Command `"kubectl exec helloworld-web-8495f4c888-jw2tg -n local-v1 -it -- /bin/bash`""
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-25
        • 1970-01-01
        • 2011-05-11
        • 1970-01-01
        • 2015-11-15
        • 1970-01-01
        相关资源
        最近更新 更多