0

I have a VB script which needs to run psexec to launch an app called md5 on a remote server. Md5 generates a hash key of a file and takes one parameter - the file path\name. I need to retrieve the has key that is generated to store in a variable. Below is the code I am using:

Set objShell = CreateObject("Wscript.Shell")
strcomputer = "remotecomputer"
tempDest = "C:\somedir"
filename = "somefile"

strCommand = "psexec -accepteula \\" & strcomputer & " -c md5.exe " & tempDest & "\" & filename & " > log.txt"
Set objExecObject = objShell.Exec("%comspec% /c " & strCommand)
Do While objExecObject.Status <> 1 'loop until previous process has finished
WScript.Sleep 100
Loop

The MD5 command is run however nothing is written to the log file. When I copy and paste strCommand (substituting all the variables for the actual data) into a cmd prompt and run it, it successfully writes the output of Md5 to the log file.

At the end of the day I just need the output of Md5, if anyone knows a better way than writing it to a log file please let me know. I have already tried using objExecObject.StdOut.Readall() to try and catch the output which resulted in random failures - sometimes it would catch the output, sometimes it wouldn't, without changing anything in the script.

VBscripter
  • 73
  • 2
  • 3
  • 7
  • See this qustion and answer: http://stackoverflow.com/questions/1399191/psexec-redirect-output-to-local-file – Frank Bollack Oct 27 '09 at 08:29
  • Hi, I already have cmd /c in my command ("%comspec% /c"). The log.txt is created on the local machine by the VB script but nothing is written to it. When I run from cmd prompt, it creates and writes to log.txt. – VBscripter Oct 27 '09 at 22:52

2 Answers2

0

Just a guess: Are you sure about what the current directory is when the script is running? Try giving an absolute path to the log file and see if it helps.

Anders Lindahl
  • 41,582
  • 9
  • 89
  • 93
  • Hi Anders The log.txt file is created, but it is blank. Nothing is written to it. Hoever when I run exactly the same command manually from cmd, the hash key is written to log.txt. – VBscripter Oct 27 '09 at 06:51
0

I found a solution for this. Instead of using the following code:

strCommand = "psexec -accepteula \\" & strcomputer & " -c md5.exe " & tempDest & "\" & filename & " > log.txt"
Set objExecObject = objShell.Exec("%comspec% /c " & strCommand)
Do While objExecObject.Status <> 1 'loop until previous process has finished
WScript.Sleep 100
Loop

I used this instead:

strCommand = "psexec -accepteula \\" & strcomputer & " -c md5.exe " & tempDest & "\" & filename & " > log.txt"
objShell.Run "%comspec% /c " & strCommand, 0, true

The script is now redirecting to log.txt properly.

VBscripter
  • 73
  • 2
  • 3
  • 7