我正在使用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运行"edit "filename""看起来只是卡住了。
我唯一能想到的是退出PSSession并运行"start \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会话,能够将窗口化应用程序转发回原始窗口环境可能是个很棒的功能...
但我仍然很满意只是编辑文件。
编辑
使用vi、emacs、cmd和edit进行测试
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
The command prompt is opened but exits immediately in PowerShell. vi and edit commands hang.
emacs produces the error message "standard input is not a tty".
To solve this issue, Jered suggests copying the file locally to edit it. Here's an improved version of their answer that uses pssessions instead of UNC:
PS C:\Users\Meredith> Invoke-Command -Session $ps -ScriptBlock {get-content c:/inetpub/myapp/web.config} > web.config
Edit the web.config file
PS C:\Users\Meredith> get-content web.config | Invoke-Command -Session $ps -ScriptBlock {set-content c:/inetpub/myapp/web.config}
The Invoke-Commands can be run in either direction, from local to remote or remote to local.