1

To delete old files on our servers we run a remote command on the server using PowerShell and Invoke-Command:

Invoke-Command {
    Param($ServerPath)
    Remove-Item -Path $ServerPath -Recurse -Force -Verbose
} -Computer servername -Credential $Credential -ArgumentList $ServerPath
# The parsed credentials are from a different user than the one who opened the
# shell

The command itself works as wanted. But this only writes the deleted files to the console, instead I want to forward it to an variable / file (preferably stored on the client executing the command).

I tried the following options without sucess:

Invoke-Command {
    Param($ServerPath)
    Remove-Item -Path $ServerPath -Recurse -Force -Verbose 4>>"%ScriptPath%\log.txt"
} -Computer servername -Credential $Credential -ArgumentList $ServerPath
Invoke-Command {
    Param($ServerPath)
    Remove-Item -Path $ServerPath -Recurse -Force -Verbose >>"%ScriptPath%\log.txt"
} -Computer servername -Credential $Credential -ArgumentList $ServerPath
Invoke-Command {
    Param($ServerPath)
    Remove-Item -Path $ServerPath -Recurse -Force -Verbose
} 4>>"%ScriptPath%\log.txt" -Computer servername -Credential $Credential -ArgumentList $ServerPath
$log = Invoke-Command {
    Param($ServerPath)
    Remove-Item -Path $ServerPath -Recurse -Force -Verbose
} -Computer servername -Credential $Credential -ArgumentList $ServerPath
$log = Invoke-Command {
    Param($ServerPath)
    return (Remove-Item -Path $ServerPath -Recurse -Force -Verbose)
} -Computer servername -Credential $Credential -ArgumentList $ServerPath

A workaround maybe could be to start a remote session to the server and execute there, but I don't want to start and cancel a remote session for just one command.

Does anyone know what I did wrong with forwarding?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Paxz
  • 2,959
  • 1
  • 20
  • 34
  • 1
    Try redirecting the verbose output on the client side, or use `-PassThru` on the `Remove-Item` command to just return the onbjects themselves and parse them on the client side to get what you need / want. – briantist Jan 08 '18 at 20:58
  • 1
    Use `Tee-Object` to log the info to a file on the remote server, and also pass it back to the local host invoking the command? – TheMadTechnician Jan 08 '18 at 21:35

1 Answers1

3

My initial guess was that your 3rd variant should have worked:

Invoke-Command {
    Param($ServerPath)
    Remove-Item -Path $ServerPath -Recurse -Force -Verbose
} 4>>"%ScriptPath%\log.txt" -Computer servername ...

However, unfortunately redirecting the verbose output stream doesn't seem to work for remote connections. To get the result you want you need to merge the verbose output stream into the success output stream inside the scriptblock, then redirect the success output stream outside the scriptblock:

Invoke-Command {
    Param($ServerPath)
    Remove-Item -Path $ServerPath -Recurse -Force -Verbose 4>&1
} >>"%ScriptPath%\log.txt" -Computer servername ...

About your other approaches:

  • #1 doesn't work because the redirection creates the file on the remote host.
  • #2 doesn't work because >> redirects the success output stream, not the verbose output stream, and it still creates the file on the remote host.
  • #4 doesn't work because $variable = ... assigns the output on the success output stream to the variable, not the output on the verbose stream.
  • #5 doesn't work for the same reason as #4. The return keyword only affects the control flow, not the output a function returns (except for output that would be created after the return statement, obviously).
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Hey @Ansgar, thanks for you answer, but sadly the mentioned method doesn't work for me. The output gets printed to the shell but not to the logfile. The result I get is the same as when i dont specify the output streams. – Paxz Jan 11 '18 at 14:54
  • @Paxz I doubt that. Please provide evidence. Also, what PowerShell versions are you running (local and remote)? – Ansgar Wiechers Jan 12 '18 at 00:27
  • I checked the servers and it seems both were running PS 2.0 while the client runs PS 5.0. We got maintanence Windows open on Tuesday and will include PS upgrades then. I will try again after the upgrade and deliver output and a better discripton of the scenario if it still fails. – Paxz Jan 12 '18 at 07:58
  • The redirection `4>&1` isn't available in PowerShell v2. Unless you can upgrade to at least v3 you're out of luck. – Ansgar Wiechers Jan 12 '18 at 10:04
  • After the upgrad to PS 3.0 it works just as you described. Thanks. – Paxz Jan 19 '18 at 13:51