1

I have written a Powershell script that uses psftp.exe to back up config files from a number of switches. The guts of the psftp loop, adapted from here, are as follows:

# set up array of psftp commands to run on each switch
$cmd = @("get $config_file_on_switch $local_filename","quit")
#
# pipe those commands to a psftp call
$cmd | & $path_to_PSFTP -l $user -pw $password $IP_addr_of_switch
#
# this is all I'd really like to see on the Powershell console:
Write-Host $name_of_switch

This works fine -- the above code is inside a loop that traverses a CSV list of switch IP addresses, and it does what it should in terms of collecting config files. But I would like to suppress the responses of the switch to the psftp commands. psftp.exe has no mechanism for disabling console output, and trying to do it in Powershell by adding | Out-Null at the end of the second line of code above does not suppress display of the switch output.

I should add that this problem occurs in the Powershell CLI window, not in the Powershell ISE console pane.

Any ideas on how to muzzle psftp in this script?

Community
  • 1
  • 1
Steve Eklund
  • 401
  • 1
  • 4
  • 9

1 Answers1

1

psftp must not be writing things to the standard output stream which is all that out-null operates on.

You can try redirecting all possible streams to the standard output stream using *>&1. For example:

Write-host "You can't see me" *>&1 | Out-Null

Or in your case:

$cmd | & $path_to_PSFTP -l $user -pw $password $IP_addr_of_switch *>&1 | Out-Null
zdan
  • 28,667
  • 7
  • 60
  • 71