Search code examples
powershellinvoke-command

Invoke-Command write file to parent machine


I have a script that connects to a remote machine using Invoke-Command and then runs a script block. This all works fine, however I would like to know if its possible to write a log file on to the parent machine. Due to firewalling on the servers the parent can connect to the child, but the child cannot connect to the parent.

At the moment when I use Out-File it creates that file on the server that I am connected to. I would like to find a way to create that file, and append to it on the server that I am connecting from.


Solution

  • Output in the scriptblock is returned to the caller. Simply pipe or redirect the output of Invoke-Command to a file.

    Invoke-Command -Computer ... -ScriptBlock {...} > 'C:\path\to\out.txt'
    

    or

    Invoke-Command -Computer ... -ScriptBlock {...} | Out-File 'C:\path\to\out.txt'