0

我想使用 wscript 通过 pscp 传输一个文件,但这段代码不起作用。它不会抛出任何错误,但也不会传输文件或输出。

Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Exec "C:\scripts\putty\pscp.exe -pw password C:\file_to_transfer.txt user@server.cz:/directory  2>> C:\Log_file.txt"

感谢帮助....

4

1 回答 1

0

您需要一个外壳来实现外壳功能(例如重定向)。所以在你的命令前加上“%comspec% /c”。

你知道 .Exec 允许读取被调用进程的 StdOut/StdErr 吗?

证据:

>> f = "c:\temp\pscp.log"
>> c = "pscp -pw pword -ls user@host:/home"
>> set s = CreateObject("WScript.Shell")
>> WScript.Echo s.Run(c & " > " & f, 0, True)
>> WScript.Echo s.Run( "%comspec% /c " & c & " > " & f, 0, True)
>> WScript.Echo goFS.OpenTextFile(f).ReadAll()
>> WScript.Echo s.Exec(c).Stdout.ReadAll()
>>
1   <-- no shell/%comspec%, no luck
0   <-- it did not fail
Listing directory /home  <-- .Run did work with shell
drwxr-xr-x    5 root     root         4096 Feb 22  2011 .
...

Listing directory /home <-- .Exec().Stdout.ReadAll() works
drwxr-xr-x    5 root     root         4096 Feb 22  2011 .
...
于 2014-04-03T14:53:30.377 回答