vscode打开linux脚本,使用 Visual Studio Code 进行远程编辑和调试

使用 Visual Studio Code 进行远程编辑和调试Using Visual Studio Code for remote editing and debugging

06/13/2019

本文内容

对于熟悉 ISE 的你来说,你可能还记得可以从集成控制台运行 psedit file.ps1 以在 ISE 中打开本地或远程文件。For those of you that are familiar with the ISE, you may recall that you could run psedit file.ps1 from the integrated console to open files - local or remote - right in the ISE.

该功能在适用于 VSCode 的 PowerShell 扩展中也可用。This feature is also available in the PowerShell extension for VSCode. 本指南将演示如何操作。This guide shows you how to do it.

必备条件Prerequisites

本指南假定你拥有:This guide assumes that you have:

可访问的远程资源(例如:虚拟机、容器)A remote resource (ex: a VM, a container) that you have access to

在其中运行的 PowerShell 和主机计算机PowerShell running on it and the host machine

VSCode 和适用于 VSCode 的 PowerShell 扩展VSCode and the PowerShell extension for VSCode

此功能适用于 Windows PowerShell 和 PowerShell Core。This feature works on Windows PowerShell and PowerShell Core.

当通过 WinRM、PowerShell Direct 或 SSH 连接到远程计算机时,此功能也适用。This feature also works when connecting to a remote machine via WinRM, PowerShell Direct, or SSH. 如果要使用 SSH,但使用的是 Windows,请查看 Win32 版本的 SSH!If you want to use SSH, but are using Windows, check out the Win32 version of SSH!

重要

Open-EditorFile 和 psedit 命令仅适用于 PowerShell 集成控制台 (由适用于 VSCode 的 PowerShell 扩展创建)。The Open-EditorFile and psedit commands only work in the PowerShell Integrated Console created by the PowerShell extension for VSCode.

用法示例Usage examples

这些示例介绍从 MacBook Pro 到在 Azure 中运行的 Ubuntu VM 的远程编辑和调试。These examples show remote editing and debugging from a MacBook Pro to an Ubuntu VM running in Azure. Windows 上的过程相同。The process is identical on Windows.

使用 Open-EditorFile 编辑的本地文件Local file editing with Open-EditorFile

启动适用于 VSCode 的 PowerShell 扩展并打开 PowerShell 集成控制台之后,可以在编辑器中键入 Open-EditorFile foo.ps1 或 psedit foo.ps1 来打开本地 foo.ps1 文件。With the PowerShell extension for VSCode started and the PowerShell Integrated Console opened, we can type Open-EditorFile foo.ps1 or psedit foo.ps1 to open the local foo.ps1 file right in the editor.

be076ee7a3590e0c3b3d304786d76731.png

备注

文件 foo.ps1 必须已经存在。The file foo.ps1 must already exist.

由此,我们可以:From there, we can:

将断点添加到装订线Add breakpoints to the gutter

153b7085f5c915a6ce80b9e82816e5e7.png

按 F5 调试 PowerShell 脚本。Hit F5 to debug the PowerShell script.

88482c1c49a10f45ff0b7974d5355925.png

调试期间,可以与调试控制台进行交互,查看左边范围中的变量,以及所有其他标准调试工具。While debugging, you can interact with the debug console, check out the variables in the scope on the left, and all the other standard debugging tools.

使用 Open-EditorFile 编辑的远程文件Remote file editing with Open-EditorFile

现在让我们进入远程文件编辑和调试。Now let's get into remote file editing and debugging. 步骤几乎相同,只需要先完成一个操作 - 将 PowerShell 会话输入到远程服务器。The steps are nearly the same, there's just one thing we need to do first - enter our PowerShell session to the remote server.

有一个 cmdlet 可以做到这一点。There's a cmdlet for to do so. 它被称为 Enter-PSSession。It's called Enter-PSSession.

该 cmdlet 的简化说明是:The watered down explanation of the cmdlet is:

Enter-PSSession -ComputerName foo 通过 WinRM 启动会话Enter-PSSession -ComputerName foo starts a session via WinRM

Enter-PSSession -ContainerId foo 和 Enter-PSSession -VmId foo 通过 PowerShell Direct 启动会话Enter-PSSession -ContainerId foo and Enter-PSSession -VmId foo start a session via PowerShell Direct

Enter-PSSession -HostName foo 通过 SSH 启动会话Enter-PSSession -HostName foo starts a session via SSH

For more information, see the documentation for Enter-PSSession.

由于我们要从 macOS 转到 Azure 中的 Ubuntu VM,因此我们将使用 SSH 进行远程处理。Since we are going from macOS to an Ubuntu VM in Azure, we are using SSH for remoting.

首先,在集成控制台中,运行 Enter-PSSession。First, in the Integrated Console, run Enter-PSSession. 当提示符的左侧显示 [] 时,即表示已连接到远程会话。You're connected to the remote session when [] shows up to the left of your prompt.

a63525cbc4cfc317f9dd9a9a10cffcea.png

现在,我们可以执行与编辑本地脚本相同的步骤。Now, we can do the same steps as if we are editing a local script.

运行 Open-EditorFile test.ps1 或 psedit test.ps1 以打开远程 test.ps1 文件Run Open-EditorFile test.ps1 or psedit test.ps1 to open the remote test.ps1 file

c52e3087a648dafda874f2abfdc4a5dc.png

编辑文件/设置断点Edit the file/set breakpoints

640d1ea4741561969a2b047fbae8e57c.png

开始调试 (F5) 远程文件Start debugging (F5) the remote file

ed05cdd10f1645c1418953a068f75831.png

如有任何疑问,可在 GitHub 存储库中提问。If you have any problems, you can open issues in the GitHub repo.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值