powershell - 如何在powershell远程 session (powershell)中编辑文件

标签 powershell editing powershell-remoting

我正在使用 powershell 远程处理连接到另一台计算机,非常好。可以做很多事,但我如何编辑文件?

PS C:\Users\guutlee> Enter-PSSession -ComputerName appprod

[appprod]: PS C:\Users\guutlee\Documents> cd \myapp

[appprod]: PS C:\myapp>



我该怎么做才能在远程机器上的文件上打开文件编辑器?

[appprod]: PS C:\myapp> edit app.config



所以编辑“文件名”似乎挂起,来自 powershell.exe 或来自 powershell_ise.exe

我唯一能想到的就是退出 pssession 并“启动\webprod\c$\inetpub\myapp\web.config”,这将打开 Visual Studio。

[appprod]: PS C:\myapp> exit

PS C:\Users\guutlee> start \agobuild\c$\myapp\app.config

PS C:\Users\guutlee> Enter-PSSession -ComputerName appprod

[appprod]: PS C:\Users\guutlee\Documents> cd \myapp

[appprod]: PS C:\myapp> myapp.exe



当然,我必须重新找到文件,希望 c$ 共享可用且可访问,并在我想继续时重新连接我的 pssession 并重新找到我的工作目录。看起来不是很优雅。

我也许可以把它包装成一个函数,但是很难把我的头围绕在它上面..

那么如何使用远程 pssession 方便地编辑文件呢?

编辑

kbrimington 的帖子让我想到了 ssh 的 -X 选项。对于powershell session 来说,能够将窗口应用程序转发回原始窗口环境可能是一件很棒的事情......

但我仍然很乐意编辑文件。

编辑

使用 vi、emacs、cmd 和编辑进行测试

PS C:\Users\Meredith> Enter-PSSession -ComputerName appprod

[appprod]: PS C:\Users\guutlee\Documents> C:\vim\vim72\vim filename.txt

[appprod]: PS C:\Users\guutlee\Documents> C:\emacs-23.2\bin\emacs.exe -nw filename.txt

emacs.exe : emacs: standard input is not a tty

+ CategoryInfo          \: NotSpecified: (emacs: standard input is not a tty:String) [], RemoteException

+ FullyQualifiedErrorId \: NativeCommandError

[appprod]: PS C:\Users\guutlee\Documents> cmd

Microsoft Windows [Version 6.1.7600]

Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\guutlee\Documents>

[appprod]: PS C:\Users\guutlee\Documents> edit filename.txt



vi 和编辑挂起(Control-C 以获得提示)

cmd 运行,产生一个提示,但立即退出回到 powershell 提示

emacs 产生错误(标准输入不是 tty)

编辑

Jered 建议将文件拉回本地进行编辑。我用 pssessions 而不是 UNC 修饰了他对复制的回答(也许这就是他的意图)

PS C:\Users\Meredith> Invoke-Command -Session $ps -ScriptBlock {get-content c:/inetpub/myapp/web.config} > web.config

edit web config

PS C:\Users\Meredith> get-content web.config | Invoke-Command -Session $ps -ScriptBlock {set-content c:/inetpub/myapp/web.config}



潜在地,我们可以在任一方向运行调用命令,本地到远程或远程返回本地。

最佳答案

如果您使用的是 Powershell 5,您可以使用名为 PSEdit 的命令。它仅适用于 ISE。

  • 首先,打开 PowerShell ISE
  • 然后使用 Enter-PSSession
  • 打开到远程计算机的远程 session
  • 然后使用 PsEdit 'filename' 编辑文件

  • 远程文件将在(本地)ISE 窗口的新选项卡中打开。

    其实我从this SO question 的评论部分找到了这个答案,但我认为如果我将其作为答案发布在这里对其他人会有所帮助。

    关于powershell - 如何在powershell远程 session (powershell)中编辑文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3432770/

    相关文章:

    iphone - UITextView 拦截对 setSelectedRange 的调用?

    java - JSON 文件 - Java : editing/updating fields values

    MySQL 插入脚本

    powershell - 如何在 PowerShell 中使用 import-csv 指定列位置

    objective-c - TableView 编辑时调用不同的 ViewController

    Powershell:脚本 block 中的 & 问题

    Powershell Get-Service 详解 "DESCRIPTION"Windows Service

    windows - 使用 powershell 在远程计算机上启动 .exe 文件

    powershell - 在Powershell中比较两个csv文件时在哪里出错?

    powershell - 如何在powershell中获取进程的 "Comand Line"属性