如何在powershell远程会话中编辑文件(powershell)

guu*_*lee 33 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>编辑app.config

所以编辑"filename"似乎挂起,来自powershell.exe或powershell_ise.exe

我唯一能想到的就是退出pssession和"start\webprod\c $\inetpub\myapp\web.config",这将打开visual studio.

[appprod]:PS C:\ myapp>退出

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会话而言,将窗口应用程序转发回原始窗口环境可能会是一件非常棒的事情......

但我仍然很高兴只是编辑文件.

编辑

使用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:标准输入不是tty

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

+ FullyQualifiedErrorId \: NativeCommandError
Run Code Online (Sandbox Code Playgroud)

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

Microsoft Windows [版本6.1.7600]

版权所有(c)2009 Microsoft Corporation.版权所有.

C:\ Users \用户guutlee \文件>

[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

编辑web配置

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

我们可以在任一方向上运行invoke-commands,本地到远程或远程返回本地.

che*_*enz 22

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

  1. 首先,打开PowerShell ISE
  2. 然后使用Enter-PSSession打开远程计算机到远程计算机
  3. 然后使用PsEdit'filename'编辑文件

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

实际上我从这个SO问题的评论部分找到了这个答案,但我认为如果我在这里作为答案发布它会对其他人有所帮助.


kbr*_*ton 1

使用基于控制台的编辑器(例如 VI 或 Emacs)进行尝试。正如我的评论中所述,我认为问题在于编辑命令绑定到窗口应用程序,而该应用程序又没有跨远程会话虚拟化。