2017-02-23 18 views
1

我寫了一個Powershell腳本,它使用psftp.exe從多個開關備份配置文件。在PSFTP環的膽量,改編自here,如下:從Powershell腳本調用PSFTP:如何靜音輸出?

# 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 

這工作得很好 - 上面的代碼是一個循環,穿過交換機的IP地址的CSV列表裏面,它做什麼,它應該在收集配置文件方面。但我想抑制交換機對psftp命令的響應。 psftp.exe沒有禁用控制檯輸出的機制,並且試圖在Powershell中通過在第二行代碼末尾添加| Out-Null來做到這一點,並不會抑制開關輸出的顯示。

我要補充的是,在PowerShell的命令行窗口中會出現這個問題,而不是在PowerShell ISE中控制檯窗格中。

關於如何在這個腳本中使用psftp的想法?

回答

1

psftp不能寫東西,這是所有out-null運行在標準輸出流。

您可以嘗試重定向所有可能的流使用*>&1標準輸出流。例如:

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

或者你的情況:

$cmd | & $path_to_PSFTP -l $user -pw $password $IP_addr_of_switch *>&1 | Out-Null 
+1

做得好; '*> $ null'是一個較短的選擇。 – mklement0

相關問題