我想使用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"
感谢您的帮助....
答案 0 :(得分:0)
您需要一个用于shell功能的shell(例如重定向)。因此,在命令前添加“%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 .
...