如何在powershell远程会话(powershell)中编辑文件 [英] how to edit a file in powershell remoting session (powershell)

查看:289
本文介绍了如何在powershell远程会话(powershell)中编辑文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am connecting to another computer using powershell remoting, really nice. can do lots, but how do I edit a file?

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

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

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

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

[appprod]: PS C:\myapp>

[appprod]: PS C:\myapp>

如何在远程机器上的文件上打开文件编辑器?

what can I do to open a file editor on a file on the remote machine?

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

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

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

so edit "filename" just seems to hang, from powershell.exe or from powershell_ise.exe

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

The only thing I can think of is back out of the pssession and "start \webprod\c$\inetpub\myapp\web.config", which would open visual studio.

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

[appprod]: PS C:\myapp> exit

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

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

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

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

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

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

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

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

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

Of course with this I have to re-find the file, hope that the c$ share is available and accessible, and the reconnect my pssession and re-find my working directory when I want to go on. It doesn't seem very elegant.

我也许可以把它包装成一个函数,但是我很难理解它..

I could maybe wrap this is a function, but having a hard time wrapping my head around that..

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

so how do I conveniently edit a file with a remote pssession?

编辑

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

kbrimington's post got me thinking me about the -X option to ssh. probably would be an awesome thing for powershell sessions to be able to forward windowed apps back to the original windowing environment...

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

but still I'd be happy just to edit the file.

编辑

使用 vi、emacs、cmd 和 edit 进行测试

tests using vi, emacs, cmd and edit

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

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:\vim\vim72\vim filename.txt

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

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

emacs.exe : emacs: 标准输入不是 tty

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

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

Microsoft Windows [版本 6.1.7600]

Microsoft Windows [Version 6.1.7600]

版权 (c) 2009 Microsoft Corporation.保留所有权利.

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

C:\Users\guutlee\Documents>

C:\Users\guutlee\Documents>

[appprod]: PS C:\Users\guutlee\Documents> 编辑文件名.txt

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

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

vi and edit hang (Control-C to get a prompt back)

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

cmd runs, producing a prompt, but immediately exits back to the powershell prompt

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

emacs produces the error (standard input is not a tty)

编辑

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

Jered suggests pulling the file back locally to edit. I embellished his answer to copying using pssessions rather than UNCs (perhaps this is what he intended)

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

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

编辑网络配置

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

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

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

Potentially we could run the invoke-commands in either direction, local to remote or remote back to local.

推荐答案

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

If you are using Powershell 5, you can use command called PSEdit. It only works from ISE.

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

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

The remote file will be opened in a new tab in your (local) ISE window.

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

Actually I found this answer from the comments section of this SO question , but I think it will be helpful for others if I post it as answer here.

这篇关于如何在powershell远程会话(powershell)中编辑文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆