views:

112

answers:

2

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.

A: 

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

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
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
What kind of editing are you doing? Does it follow a pattern?
Jered Odegard
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