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

妖精的绣舞 提交于 2019-12-02 17:56:48
chenz

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.

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

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.

Henrique

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.

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

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.

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.

Scott Alan Miller

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!