我使用powershell FTP Client Module设置FTP连接以检索某些文件。
我知道我可以用这种方式捕获命令的输出:
$output = (command) | Out-String
我想将-Verbose
属性的输出重定向到一个变量中..我做了类似的事情,但是FTP连接无法创建,我的文章就停止了。< / p>
$output1 = (Set-FTPConnection -Server $ftp_server -Credentials $mycreds -Session $session)
$output2 = (
Get-FTPChildItem -Path $ftp_path -Session $session -Verbose | % {
$ftpFile = "ftpPathIn/$($_.Name)"
Get-FTPItem -Path $ftpFile -LocalPath $PathToFile -Overwrite -Session $session -Verbose
} ) | Out-String
PS:没有尝试重定向变量中的输出,脚本运行得很好。所以我怀疑错误是我尝试重定向$output
中的输出的方式。
如何在变量$ output中重定向输出?
答案 0 :(得分:2)
您可以尝试这样的事情:
$output2 = $(
Get-FTPChildItem -Path $ftp_path -Session $session -Verbose | % {
$ftpFile = "ftpPathIn/$($_.Name)"
Get-FTPItem -Path $ftpFile -LocalPath $PathToFile -Overwrite -Session $session -Verbose
} ) 4>&1
有关流重定向语法的说明,请参阅Get-Help about_Redirection
。