none
Remotely editing text files in Windows WinRM/PSExec/Powershell/cmd RRS feed

  • Question

  • Hi!

    How can I remotely edit a text file in Windows?

    I am running and administering a number of Windows 7 workstations and Windows Server 2012 machines. I would like to be able to remotely edit text files on any of these machines. I can connect to any of these machines via psexec or powershell remoting. I can also connect via rdp, but for the workstations I would rather not kick the user off unless I absolutely have to.

    For example, we manage state configuration using Salt. Local configuration files are stored in C:/salt/conf. I would like to be able to edit these config files on the fly.

    This question on Stack Overflow covers the same topic, but none of the answers are particularly satisfying. Some of the possible solutions are:

    • Remote Desktop: as I say, I would rather not have to kick a user off their workstation
    • Edit a file over a UNC path: this only works for files on a share, and setting up arbitrary shares for quick edits seems like a bad idea.
    • Install some manner of ftp or ssh server on all the target computers. This would certainly work, but it is it necessary with WinRM already active? I feel like installing and configuring extra software across all the machines in my organisation would be a last resort.
    • Edit with a command line editor (e.g. vim, emacs) over a remote powershell session. This doesn't work. I don't understand the technical details, but a powershell remote session isn't interactive in the same way that ssh connections are.
    • Edit using powershell's -replace operator, out-file and add-content cmdlets. This works, but is hardly the same as a fully featured text editor.
    • Retrieve content from the remote, edit locally, and post back to the remote. This works, and is arguably the best solution I've found. Code sample from SO:
        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}

    The last two options are the workflow that I've been using for now. Regex replacement for very simple changes, and copying to local and editing there for more complicated ones. Neither is ideal, but they work. It just feels incredibly old fashioned.

    I'm considering writing a plugin to my editor of choice (vim) to perform the remote fetch and save, to make my workflow a little bit smoother. Before I try that, I just want to know if there's anything that I've missed, or misunderstood.

    Cheers


    • Edited by blaffoy Friday, November 14, 2014 11:39 AM typo
    Friday, November 14, 2014 11:29 AM

Answers

  • What is the "redirector"?

    I posted the same question to Stack Overflow, and was told about administrative shares. I wasn't aware of this, and it's exactly what I wanted. I can open the filetree of a remote machine with the path \\machinename\c$\. Perfectly simple, now that I know the answer.

    Friday, November 14, 2014 3:33 PM

All replies

  • You can use below approach (string hello in c:\Temp\test.txt is replaced with hai ) -

    invoke-command -computername servername -Scriptblock { $data = ( get-content -Path c:\Temp\test.txt | Foreach-Object {$_ -replace "hello", "hai"} ) ; Set-content $data -Path c:\Temp\test.txt   }

    Friday, November 14, 2014 12:28 PM
  • It doesn't sound to me like you're missing anything. IMO the easiest option is to access the file over the network using the redirector, but if you don't want to set that up and would rather do something else, that's up to you.

    -- Bill Stewart [Bill_Stewart]

    Friday, November 14, 2014 3:29 PM
    Moderator
  • What is the "redirector"?

    I posted the same question to Stack Overflow, and was told about administrative shares. I wasn't aware of this, and it's exactly what I wanted. I can open the filetree of a remote machine with the path \\machinename\c$\. Perfectly simple, now that I know the answer.

    Friday, November 14, 2014 3:33 PM
  • Yes, that's what I was referring to - the network redirector (a UNC path of a remote file).

    If admin shares are available, that's by far the simplest solution.


    -- Bill Stewart [Bill_Stewart]

    Friday, November 14, 2014 3:34 PM
    Moderator
  • Right, I had already tried opening \\machinename\c\path\to\file. Obviously this didn't work. Without knowing the term "administrative share" none of my research turned up the idea of \\machinename\c$\path\to\file.

    Thanks for your help.

    Friday, November 14, 2014 3:41 PM