Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 557
guutlee Avatar asked Aug 08 '10 01:08

guutlee


People also ask

How do you edit the contents of a file in PowerShell?

To edit a file. txt in PowerShell, use the command notepad file. txt to open a graphical editor on Windows. If you need a simple file edit in your terminal without a graphical editor and without installation, you can use the command echo 'new content' > file.

How do I run a PowerShell script on a remote computer?

To run a script on one or many remote computers, use the FilePath parameter of the Invoke-Command cmdlet. The script must be on or accessible to your local computer. The results are returned to your local computer.

How do I edit a PowerShell script?

Opening and Editing Scripts Choose Open on the File menu (or press Ctrl+O) and select a PowerShell script from the Open dialog box. Use the mouse to drag and drop a PowerShell script onto an ISE window or shortcut icon. Right-click on the PowerShell script in Windows Explorer and choose Edit.

How do I use WinRM PowerShell?

In Windows 7 or 8, hit Start, and then type “powershell.” Right-click the result and choose “Run as administrator.” This command starts the WinRM service, sets it to start automatically with your system, and creates a firewall rule that allows incoming connections.


1 Answers

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.

like image 96
chenz Avatar answered Sep 29 '22 10:09

chenz