Announcement

Collapse
No announcement yet.

Show output of external command run from script

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Show output of external command run from script

    I have a script which Robocopies folders & files from a server to another. It runs Robocopy which displays a blank Command box until it finishes running. Is there anyway to display the output of Robocopy as it is running, in the Command box?

    I just want to make sure it's doing something simply by looking at the screen.

    Here's the code:
    Dim Quotes

    set objShell= CreateObject("WScript.Shell")
    Result = objShell.Popup("Copy Started " & Now,5)

    Quotes = Chr(34)

    ' SourceDir = "\\Dande-srv\VOL1\SHARED"
    ' SourceDir = Quotes & "\\Dande-srv\vol1\SHARED\GMP" & Quotes
    SourceDir = "N:\"
    DateTimeStamp = "[" & datepart("yyyy", Now) & "-" & DatePart("m", Now) & "-" & DatePart("d",Now) & "]"
    ' DestRootDrive = "F:\"
    DestRootDrive = "C:\User\Dump\"
    DestDir = Quotes & DestRootDrive & DateTimeStamp & Quotes
    ' Options = "/E /ETA /NP /V /PURGE /TEE /R:100 /W:10 /LOG:" & DateTimeStamp & "_LogFile.Log"
    Options = "/E /ETA /NP /V /PURGE /R:100 /W:10 /LOG:" & DateTimeStamp & "_LogFile.Log"

    StrCommand = "Robocopy " & SourceDir & " " & DestDir & " " & Options

    objShell.CurrentDirectory = DestRootDrive
    ' Run Robocopy and wait for it to finish.
    Set ErrorCode = objShell.exec(StrCommand)

    ' Wait for application to Exit
    Do While ErrorCode.Status = 0
    WScript.Sleep 1
    Loop
    Set objStdOut = ErrorCode.StdOut

    WScript.Echo objStdOut.ReadAll

    Result = objShell.Popup("Copy Ended " & Now,5)
    WScript.Quit(0)
    Thanks.
    |
    +-- JDMils
    |
    +-- Regional Systems Engineer, DotNet programmer & Jack of all trades
    |

  • #2
    Re: Show output of external command run from script

    AFAIK, that cannot be done.
    With the method you are using the output(/and input) is forwarded to StdOut (and StdIn) and not to the screen.

    Since you are using the stdOut only for showing the results on screen at the end. In that case you could also use "the Run method with the parameters ,1,True" instead of using the Exec method. This will give you a direct output to the screen. Run the command string with %comspec% /K then the command-box- does not close at the end, so you see the results and the progress.

    Alternatively, when no-box at all also is an option, then use also the Run method.
    With the Run method you are able to run the command invisible (in a hidden box) and wait on return, by using the parameters ,0,True. Run the command with %comspec% /c and Pipe ( > )the output to a >tempfile. Show the content of the tempfile (ReadAll) in a messagebox.

    Instead of the Run method you can also use a WMI class Win32_ProcessStartup method. This method runs a command with kind of similar options as using the Run method, but here you can get the PID of the created proccess returned and you can set the priority of the the proccess you create. http://www.microsoft.com/technet/scr....mspx?mfr=true . With this method you can also run the command by using %comspec% /c and pipe the output to a textfile or using %comspec% /k to leave the box open.


    \Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Show output of external command run from script

      Rems,

      The copy of Robocopy I had does not show copy progress even when run manually at the command line.

      I upgraded my copy of Robocopy to XP10 which now adds a new command line option "/TEE" which displays Robocopy progress on the screen.

      My next question is this:

      Q. When run from the command line, it works brilliantly. When run as a Scheduled Task while I am logged into the server, it comes up in Task Manager, but doesn't do anything. When Robocopy is executed, it creates a log file. it's not doing this. Why is my script apparently just sitting there and how can I debug the problem?

      Here's my updated code. i included the TestRun variable so that I could adapt the code depending on whether I was testing it on my workstation or running it live on the server.

      Note: It is copying folder & files from a Novell server. I am logged in as a user with Novell access. The scheduled task is running n the credentials of the same user.

      Option Explicit

      Dim objShell

      Dim Quotes
      Dim TestRun
      Dim Result
      Dim SourceDir
      Dim DestRootDrive
      Dim DateTimeStamp
      Dim DestDir
      Dim Options
      Dim StrCommand
      Dim ReturnCode

      set objShell= CreateObject("WScript.Shell")

      TestRun = False

      Quotes = Chr(34)
      Select Case TestRun
      Case True
      ' Run TEST off the workstation.
      Result = objShell.Popup("Copy Started " & Now,5)
      SourceDir = Quotes & "\\Dande-srv\vol1\SHARED\Documentation" & Quotes
      DestRootDrive = "C:\User\Dump\"
      Case False
      ' Run LIVE off the server.
      SourceDir = "\\Dande-srv\VOL1\SHARED"
      DestRootDrive = "F:\"
      End Select

      DateTimeStamp = "[" & datepart("yyyy", Now) & "-" & DatePart("m", Now) & "-" & DatePart("d",Now) & "]"
      DestDir = Quotes & DestRootDrive & DateTimeStamp & Quotes
      Options = "/E /ETA /V /PURGE /TEE /R:100 /W:10 /LOG:" & DateTimeStamp & "_LogFile.Log"

      StrCommand = "Robocopy " & SourceDir & " " & DestDir & " " & Options

      objShell.CurrentDirectory = DestRootDrive
      ' Run Robocopy and wait for it to finish.
      ReturnCode = objShell.Run(StrCommand,1,True)
      ' wscript.echo strcommand

      if Testrun Then Result = objShell.Popup("Copy Ended " & Now,5)
      WScript.Quit(0)
      Thanks.
      |
      +-- JDMils
      |
      +-- Regional Systems Engineer, DotNet programmer & Jack of all trades
      |

      Comment


      • #4
        Re: Show output of external command run from script

        A sheduled task will logon in a very basic environment (like using Runas). When the scheduled task runs, the program you've scheduled runs as if it were started by the user you specified, with that user's security context. It run in its own 'basic' user and system environment, no mappings are available. Also the mappings of other (current) logons are not available.

        That is why you cannot use drive that are mapped in a script, when you are planning to run it with a '2d logon'. -And- the user you specified, must have permissions to access the other computers, and must have sufficient permissions for accessing all the local and the remote resources you use in the script.

        Could it be that F: is a mapped drive here?
        In that case you have to use the unc-path instead. But then the 'currentdirectory' cannot be set any more for objShell.run and c:\windows will de used as the currentdirectory. So you have to modify the commandstring too:

        Code:
        Const QUOTES = """"
        
        SourceDir = "\\Dande-srv\VOL1\SHARED"
        DestRootDrive = "\\User-srv\dump"    '???
        
        'For better date formatting (and only 1 call to Date function) be... 
        dt = date()
        DateTimeStamp = "[" & Year(dt) & "-" & Right(100+Month(dt),2) _
                     & "-" & Right(100+Day(dt),2) & "]"
        
        DestDir = quotes & DestRootDrive & "\" & DateTimeStamp & quotes
        LogFile = quotes & DestRootDrive & "\" & DateTimeStamp & "_LogFile.Log" & quotes
        
        Options = "/E /ETA /V /PURGE /TEE /R:100 /W:10 /LOG:" & LogFile
        
        strCommand = quotes & DestRootDrive & "\Robocopy.exe"" " 
        strCommand = strCommand & quotes & SourceDir & quotes 
        strCommand = strCommand & " " & DestDir & " " & Options
        
        ReturnCode = objShell.Run("%comspec% /c " & strCommand,1,True)
        \Rems

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment

        Working...
        X