how to edit a file in powershell remoting session (powershell)

Viewed 53597

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

[appprod]: PS C:\Users\guutlee\Documents> cd \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> edit app.config

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

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> 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

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..

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

EDIT

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.

EDIT

tests using vi, emacs, cmd and 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

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 and edit hang (Control-C to get a prompt back)

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

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

EDIT

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

edit 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.

10 Answers

Inspired by the answer provided by @chenz, this can also be done using Visual Studio Code when inside the Powershell Integrated Shell. I tried this with Powershell 7 over SSH remoting, but should also work with WinRM remoting as well.

Enter-Session ...
[RemoteSystem] PS> psedit service.log

The filename will follow this pattern:

C:\users\dev\appdata\local\temp\2\pses-15960\remotefiles\1702137071\winboxname\service.log

I've been looking into this quite thoroughly given the limitations of PSRemoting and the possible work arounds or solutions.

Console editors do not work as they are native (exe) commands and are character based. (I'm wondering if cmd over ssh works with these) It needs to be a cmdlet to properly interact with the console host over a psremote session.

A PSRemote session is line based. You edit the line locally push enter and then it is sent to the remote end, the command is executed and the result returned. So it does support line interactivity but not character interactivity.

Given all these limitations, GNU's ed appears to be a perfect fit. I'm surprised that no one has looked at implementing it as a cmdlet.

I decided to investigate this further, I checked that the powershell cmdlet read-host works and then the C# equivalent PSHostUserInterface.ReadLine() work over a PSRemote session and they do, which is all that is needed to make a working line editor.

So here's what I've done so far. I could probably use some help on the more exotic regex code.

https://github.com/silicontrip/ps-ed

I've used this to edit files via a psremote session. (I wouldn't go editing large source files with it, but for the odd small config file or ps1 script it is perfect.)

Make sure you are familiar with GNU ED before using this cmdlet. It'd be like trying to use VI for the very first time, otherwise.

Related