38

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.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
guutlee
  • 423
  • 1
  • 5
  • 7
  • This doesn't answer how to conveniently edit a file, but is an explanation of the hanging behavior, so I'll post as a comment. Your session appears to hang because a remote session will not virtualize windowed applications for you; that is, your edit session would be visible to you in Remote Desktop if you were connected via terminal services. – kbrimington Aug 08 '10 at 02:37
  • it won't work very well either with interactive console apps. – x0n Aug 09 '10 at 18:55

10 Answers10

28

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

  1. So first, open PowerShell ISE
  2. Then open remote session to the remote computer using Enter-PSSession
  3. Then edit the file using PsEdit 'filename'

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

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.

Community
  • 1
  • 1
chenz
  • 730
  • 7
  • 13
4

Can you not pull the file locally, edit it and post it? I know this is tedious and not elegant but it seems editors are presently having issue with remote sessions.

E.g.,

Get-Content REMOTE\Filename.txt > LOCAL\Filename.txt

Make your changes locally and then

Set-Content -path REMOTE\Filename.txt -value (get-content LOCAL\Filename.txt)

EDIT

Also if you are only replacing certain instances you can do this pretty easily.

E.g.,

Get-Content REMOTE\Filename.txt | foreach-object { $_ -replace "OLD", "NEW" } | Set-Content REMOTE\Filename.txt
Jered Odegard
  • 213
  • 4
  • 12
  • A good alternative to my initial idea of opening a local editor on a remote share. But it reverses the dependency by using copy to a local share and then again local editor, and a copy back to the original location. I'd really like to lose the dependency on a file share so that I could expose only winrm and not any additional services. – guutlee Aug 17 '10 at 03:48
  • What kind of editing are you doing? Does it follow a pattern? – Jered Odegard Aug 17 '10 at 04:12
  • Mostly ad-hoc, I guess; maybe adding a paragraph of text to an html file; maybe updating a connection string in a web app; maybe changing a powershell script to add some functionality. – guutlee Aug 17 '10 at 04:24
  • The EDIT will only work if you put parantheses around the Get-Content to be shure the file isn't opened by it while writing. See help (example 3) [Technet Set-Content](https://technet.microsoft.com/en-us/library/dd347736.aspx) – fadanner Jul 07 '15 at 08:12
4

First, create a temp folder on the local machine (LOCALTEMPFOLDER). Then, use the following function:

function vimrem {param([parameter(position=0,mandatory=$true)][string]$Session, [parameter(position=1,mandatory=$true)][string]$Path)

$TempFile = split-path -path $Path -leaf

copy-item -fromsession $Session -path $Path -destination LOCALTEMPFOLDER\$TempFile

vim $LOCALTEMPFOLDER\$TempFile

copy-item -tosession $Session -path LOCALTEMPFOLDER\$TempFile -destination $Path

remove-item -path LOCALTEMPFOLDER\$TempFile

}

This should work, but you will have leave an interactive session before using this function.

BSMP
  • 4,596
  • 8
  • 33
  • 44
Henrique
  • 41
  • 1
  • 1
    You don't need to state example questions before your answer. Just the answer and any accompanying explanation. – AfroThundr Feb 12 '18 at 21:40
4

After much digging around, I found something that seems relevant in the powershell help documentation. At the powershell prompt, type:

help about_remote_troubleshooting

At the very end of the help file that is displayed, there is a section entitled 'TROUBLESHOOTING UNRESPONSIVE BEHAVIOUR', which states:

TROUBLESHOOTING UNRESPONSIVE BEHAVIOR

This section discusses remoting problems that prevent a command from completing and prevent or delay the return of the Windows PowerShell prompt.

HOW TO INTERRUPT A COMMAND


Some native Windows programs, such as programs with a user interface, console applications that prompt for input, and console applications that use the Win32 console API, do not work correctly in the Windows PowerShell remote host.

When you use these programs, you might see unexpected behavior, such as no output, partial output, or a remote command that does not complete. To end an unresponsive program, type CTRL + C. To view any errors that might have been reported, type "$error" in the local host and the remote session.

Thus it would seem even non-GUI console applications such as VIM won't work unfortunately. Anyone care to shed a little light on why this might be the case and/or whether it can be worked around? I would REALLY love it if I could use vim over powershell remoting.

dtm
  • 41
  • 2
  • 3
    I can shed some light on why: Console IO on Windows is closer to performing ioctls on a device (out-of-band signalling) than it is to streaming control codes. Take a look at how the API is structured at http://msdn.microsoft.com/en-us/library/windows/desktop/ms682088%28v=vs.85%29.aspx . Contrast this with how things like, say, VT100 emulation works (embedded escape sequences in-band). PowerShell operates on a streaming basis for I/O, and not on a console. – user314104 Oct 21 '13 at 21:27
4
PowerShell_ISE.exe \\REMOTE\...\File.txt 

will load and edit the file directly and save it back to the remote computer in one step and, since its command line, easy to build functions using it. Doesn't get around sharing problems, but the easiest way I've found.

Chris F Carroll
  • 11,146
  • 3
  • 53
  • 61
Lor
  • 41
  • 1
1

Try it out using a console-based editor such as VI or Emacs. As in my comment, I think the problem is that the edit command is bound to a windowed application which, in turn, is not virtualized across a remote session.

kbrimington
  • 25,142
  • 5
  • 62
  • 74
  • I believe that edit.com is the console editor and not a windowed application. I did install emacs, (well, I already had emacs installed, trying to get away from my UNIX roots with powershell :) ). this is the output > [appprod]: PS C:\emacs-23.2> .\bin\emacs.exe -nw .\README.W32 > emacs.exe : emacs: standard input is not a tty > + CategoryInfo : NotSpecified: (emacs: standard input is not a tty:String) [ > ], RemoteException > + FullyQualifiedErrorId : NativeCommandError – guutlee Aug 08 '10 at 03:32
  • Bummer. Does VI get you anywhere? I can run `cmd` from a remote session, so I was perhaps-too-optimistic that other console apps would work the same. – kbrimington Aug 08 '10 at 03:55
  • I ran cmd from a remote session, it ran, but immediately exited back to powershell. Is this different behavior that you are seeing? I tried vi, it hung just like edit did. my test are summarized above. – guutlee Aug 08 '10 at 15:52
  • I'm fairly certain, but I should get back to the office to check. I seem to recall using cmd in a remote session while controlling my SharePoint servers. – kbrimington Aug 08 '10 at 16:15
  • By the way, you can probably bank on the c$ share being accessible, as it is only available to administrators, and you must be an administrator to use remoting. This doesn't solve editing through a remote session, but perhaps it increases confidence that you can rely on the editor from a local session. – kbrimington Aug 08 '10 at 16:18
  • Perhaps now my servers are only accessible remotely via terminal services, I don't allow remote file shares. I'd like to use powershell or winrm in place of terminal services. – guutlee Aug 17 '10 at 03:50
1

I try all the above suggestion and even other microsoft related solution but none works the way you and I want --"full interactive and responsive shell"--. If you really want to have the ssh experience that unix users have from the beggining of time i recomend you install an ssh server. I personally use freesshd, you can find it here http://www.freesshd.com and instructions of how to configure it here http://www.windowsnetworking.com/articles_tutorials/install-SSH-Server-Windows-Server-2008.html. After you make all that it says in the instructions you only need to use any ssh client app to connect to your computer and use powershell full interactive. Vim, edit, emacs or whatever you use to edit a file is going to work without any problem.

i encourage you to not waste your time with psremoting, telnet, winrs, psexec, trying to achieve what a real interactive shell provide (i already lost it, T_T). Try that ssh server an see for your self.

mjsr
  • 7,410
  • 18
  • 57
  • 83
  • Powershell sessions are designed to be stateless, they were never meant to be 'full interactive and responsive shell'. There are advantages to *nix shells and powershell, and this happens to be one of the disadvantages. That said, there have been some good suggestions as to ways around it. – Shafiq Jetha Apr 17 '15 at 21:35
1

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

craigdfrench
  • 972
  • 10
  • 20
0

I got nano to work easily over SSH to PowerShell. It's available in Chocolatey so you can just do...

choco install nano -y

Then you can just do...

nano filename
Tunaki
  • 132,869
  • 46
  • 340
  • 423
0

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.

silicontrip
  • 906
  • 7
  • 23