Does PSCP work with PowerShell?

若如初见. 提交于 2019-12-11 10:45:38

问题


I have a PowerShell script that produces a text file. At the end, I would like to copy this file to a Linux server. From CMD.EXE, I can use PSCP (from Putty), it works and copies the file. But from PowerShell, either interactively or from a PowerShell batch, PSCP has no visible effect: no error messages and the file is not copied. Even if I run simply .\PSCP.EXE without arguments, on the CMD command line it displays the options, but from PowerShell it does nothing. Can PSCP be used from inside PowerShell?


回答1:


Executing a program from within PowerShell should work identically to CMD, but depending upon how that program produces its output (does it write to STDOUT, STDERR, other?) that may behave differently.

I've been using Rebex's components for FTPS & SFTP within .NET apps & PowerShell scripts; the SFTP package includes an SCP class. Yes, it costs money, but depending upon your usage it may be worthwhile.




回答2:


Yes - most any executable can be called from PowerShell. There isn't anything peculiar about pscp.exe in this regard. You may need to preface it with the call operator - the ampersand - &:

PS C:\>& "C:\Program Files (x86)\Putty\pscp.exe" -V
pscp: Release 0.62

The above is direct output from my PowerShell prompt. The call operator is particularly helpful if the path to your executable contains spaces - the call operator is used to tell PowerShell to treat what would be considered a string as something it should try to execute instead.

Please include the full command your are trying to execute as it will help in providing a better answer. You may have a problem with your PATH variable or something else weird if you don't get any output.




回答3:


If using pscsp from inside a script, e.g. perl

  • no ampersand

  • quote like this "my password"

e.g.

"C:\Program Files\Putty\pscp.exe" -C -p -pw "password"  /local_dir/file_to_copy  user@hostname:/remote_directory

in perl (beware that \ is an escape char in a "string" )

$cmd = q("C:\Program Files\Putty\pscp.exe" -C -p -pw "password"  /local_dir/file_to_copy  user@hostname:/remote_directory);

system($cmd);



回答4:


Just attempted to automate PSCP from PowerShell. Remember to use pscp's -batch parameter so that, should you do something like enter the wrong password, you won't get asked for input.

$Cmd = "pscp -l username -pw password -batch c:\folder\file.txt server:/home/user1"
Invoke-Expression "& $( $Cmd )"

Otherwise your script will just grind to a halt.



来源:https://stackoverflow.com/questions/13686686/does-pscp-work-with-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!