我正在使用 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。
远程文件将在(本地)ISE 窗口的新选项卡中打开。
其实我从this SO question 的评论部分找到了这个答案,但我认为如果我将其作为答案发布在这里对其他人会有所帮助。
关于powershell - 如何在powershell远程 session (powershell)中编辑文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3432770/