• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3590
  • Last Modified:

Capture DOS Output with VB (don't use > like "command.exe > log.txt") - Is it possible?

Experts,

I'm in need of capturing DOS output, and relaying it into a log file, via VB.

Unfortunately, the program I'm using is PSExec, and although it works directly at the command prompt, by doing a "psexec.exe -s -c -f \\machinename \\servername\share\command.exe >> logfile.txt 2>&1"   -   I'm running this command from a vb Shell, and it drops the carrots for the logfile completely.

I'm calling on PSExec from within an MS Access DB Form.   PSExec gets executed as expected, but unfortunately - I'm in some serious need to be able to log if it's successful or not.

Is it possible to redirect the output of a DOS session to a text file, versus the standard Monitor output using VB?

I've searched quite ab it on this topic, and haven't found much info.  Although, I did catch a forum at some point that there was a PERL programmer that had run into the exact same problem with PSExec not logging anything if it's run from a script.  - Although in that thread, there didn't appear to be a solution for the PERL gentlemen.

Also, please note that dropping PSExec into a batch file and calling the batch isn't an option at this point, I can only plan on having it called from within the VB itself (as the DB contains about a gazillion and a half different records, etc. on which to call PSExec.)

Any help that can be provided would be greatly appreciated, and as always - thanks for helping.
0
usslindstrom
Asked:
usslindstrom
  • 5
  • 5
  • 2
  • +1
4 Solutions
 
DRY_GINCommented:
im not sure if thats an easy option, but you can attach your code to a function within PSExec (whatever it is) like all debuggers do, and catch all outputs before they go to command line output
where is bunch of programs available to do that, so it is theoretically possible.

you can also replace command line  (cmd) with your own implementation of cmd
something like that http://sourceforge.net/projects/console
and such third party replacement for cmd will give you extra control on output and logging


0
 
CyberLexCommented:
Hi there

this is  vb.net 2008

For Testing purposes, i did call the function with:
MsgBox(getOutput("cmd.exe", "/C dir", ""))

(cmd.exe /C dir - start cmd console and processes a dir command, then exits, see cmd /?)

cheers
CyberLex
 Public Function getOutput(ByVal proc As String, ByVal parameters As String, ByVal dir As String) As String
 
        Dim p As Process = New Process
 
        p.StartInfo.FileName = proc
 
        If Not (dir = "") Then
            p.StartInfo.WorkingDirectory = dir
        End If
 
        p.StartInfo.Arguments = parameters
        p.StartInfo.UseShellExecute = False
        p.StartInfo.RedirectStandardOutput = True
 
        ' start the process 
        p.Start()
 
        ' read all the output
        Dim out As String = p.StandardOutput.ReadToEnd
 
        ' wait for proc to exit 
        p.WaitForExit()
        Return out
 
    End Function

Open in new window

0
 
usslindstromAuthor Commented:
DRY_GIN - Actually that would go perfectly right now - some dry gin.  :)   - Thanks for the suggestion on replacing cmd with some other ap that redirects output,  and I may end up going that route.  If I did, I'd need clearance from about a million and a half people that make more money than me (I'm just the lolely little fix-it guy working on a gov PC).  :)     I'll definately try out the link you provided on my home network and see what security vulnerabilities using that app could introduce to the work computer and then I'll bring it up to the software approval ppl.  Thnx for the info.

CyberLex - that code may be exactly what will work in this situation.  Could I get some assistance in massaging it to get the output on my end?

Meaning, I have a recordset that gets called on     rst("CommandLine")     and I convert that into a string     VariableCommandLine = rst("CommandLine")             Once the items is converted into a string, I can pretty much do whatever I need to on it, using          Eval(VariableCommandLine)       or      Shell VariableCommandLine , vbMinimizedFocus         or something similar.

Using that function, where would I put the reference to my string VariableCommandLine?

Sorry for my basic understanding of VB.  The more I get into it, the more I realize I'm absolutely horrible at programming.  :)
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
CyberLexCommented:
Cheers

I guess you will have to try either

getOutput("psexec.exe","-s -c -f \\machinename \\servername\share\command.exe","")

or try

dim param as string = "psexec.exe -s -c -f \\machinename \\servername\share\command.exe"
getOutput("cmd.exe","/C " & chr(34) & param & chr(34),"")

its 4:30 am here now, sheesh :) need some sleep grin grin
CyberLex
0
 
CyberLexCommented:
sorry, better with snippet box :)
getOutput("psexec.exe","-s -c -f \\machinename \\servername\share\command.exe","")
 
or try
 
dim param as string = "psexec.exe -s -c -f \\machinename \\servername\share\command.exe"
getOutput("cmd.exe","/C " & chr(34) & param & chr(34),"")

Open in new window

0
 
BALMUKUND KESHAVCommented:
Can you put your vb sheel srcipt , it should work as its in dos. There must be some problem in using sheel command in vb.
If the following works fine in dos then put this in a batch file and call the batch file from vb using sheel command., if machinename, servername,share etc is variable type then pass the variables in a string and reconstruct batch file every time before its calling in vb.

 "psexec.exe -s -c -f \\machinename \\servername\share\command.exe >> logfile.txt 2>&1"  

Bm Keshav

0
 
usslindstromAuthor Commented:
Sorry it took so long to get back on this.

I attached the code as requested, to call the PSExec command.

As an example, here's a cut/paste from one of the tables that uses PSExec:

CommandLine
d:\iava\psexec\psexec.exe -s -c -f \\UWDFEOKG8CCSF1 \\feoktd01\patches\AdbeRdr90_en_US.exe /s >> d:\iava\psexec\psexeclog.txt 2>&1

-------------------------------------

The Form's recordset is the query that provided that string.

Whenever I cut/paste that command line string directly into a cmd prompt - the log is created perfectly.  However, when it's run directly from the VB on the form, no log gets created.

I'm trying out CyberLex's code as we speak - but my limited understanding of VB will add a bit of time to test it out and see if I can get it working.
Private Sub PSExecExecute_Click()
 
DoCmd.OpenForm "PleaseWait"
 
Dim rst As DAO.Recordset
Set rst = Me.RecordsetClone
Dim strFileName As String
Dim strText As String
 
rst.MoveLast
rst.MoveFirst
 
    strFileName = "D:\IAVA\PSExec\PSExecLog.txt"
    strText = strText & vbCrLf
    
    If Dir(strFileName) <> "" Then
      Kill strFileName
    End If
    
    Dim ff As Long
    
    ff = FreeFile
    Open strFileName For Output As #ff
    Print #ff, strText
    Close #ff
    
    Const ForReading = 1
    Const ForAppending = 8
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objLogFile = objFSO.OpenTextFile(strFileName, ForAppending, True)
    
    objLogFile.WriteLine ("Patching started on:  " & Now & vbCrLf)
 
Do Until rst.EOF
 
  Dim VariableCommandLine As String
  
  VariableCommandLine = rst("CommandLine")
  
  If VariableCommandLine Like "*psexec*" Then
 
    objLogFile.WriteLine ("Attempting " & VariableCommandLine & " at " & Now & vbCrLf)
    
    Dim wsh
    Set wsh = CreateObject("WScript.Shell")
    wsh.Run VariableCommandLine, 0, 1
 
    'Shell VariableCommandLine, vbMinimizedFocus
    
  Else
    Eval (VariableCommandLine)
    
    objLogFile.WriteLine ("Command Line Switches unavailable.  Relayed VB:  " & VariableCommandLine & vbCrLf)
  End If
    
  rst.MoveNext
 
Loop
 
DoCmd.Close "PleaseWait"
 
FollowHyperlink strFileName

Open in new window

0
 
CyberLexCommented:
:=)
dim param as string = "d:\iava\psexec\psexec.exe -s -c -f \\UWDFEOKG8CCSF1 \\feoktd01\patches\AdbeRdr90_en_US.exe /s"
getOutput("cmd.exe","/C " & chr(34) & param & chr(34),"") 
 
or
 
getOutput("d:\iava\psexec\psexec.exe","-s -c -f \\UWDFEOKG8CCSF1 \\feoktd01\patches\AdbeRdr90_en_US.exe /s","")

Open in new window

0
 
usslindstromAuthor Commented:
Please forgive me, but I'm getting a couple errors in Access with those codes provided:

In the Visual Basic editor, I'm getting "Compile error:  Expected =" from the getOutput line:
    getOutput("cmd.exe","/C " & chr(34) & param & chr(34),"")

And from the first line, I'm receiving "Compile error:  Expected: end of statement".

Please advise on any troubleshooting techniques I can use here.
0
 
CyberLexCommented:
Greetings!

I'm sorry I must have overlooked that it is VBS and not VB. The code I posted is Visual Basic VB.NET 2008, I will have a look at it later today, but I'm not very familiar with VBS tho :)

Alex
0
 
BALMUKUND KESHAVCommented:
usslindstrom:
Are you able to generate  "D:\IAVA\PSExec\PSExecLog.txt" file with following data:
Attempting " & VariableCommandLine & " at " & Now & vbCrLf)

If not put a message box between

  VariableCommandLine = rst("CommandLine")
msgbox "Record set data1 :"+variablecommandline
 
  If VariableCommandLine Like "*psexec*" Then
msgbox "Record set data2 :"+variablecommandline
     objLogFile.WriteLine ("Attempting " & VariableCommandLine & " at " & Now & vbCrLf)
   
    Dim wsh
    Set wsh = CreateObject("WScript.Shell")
    wsh.Run VariableCommandLine, 0, 1
 
    'Shell VariableCommandLine, vbMinimizedFocus
   
  Else
    Eval (VariableCommandLine)
   
    objLogFile.WriteLine ("Command Line Switches unavailable.  Relayed VB:  " & VariableCommandLine & vbCrLf)
  End If
'CLOSE YOUR TEXT FILE
CLOSE # strfilename

TRY TO DEBUG ???????/
IF ITS NOT SOLVED THE PROBLEM ,SIMPLY CREATE A BATCH FILE IN DOS AND TRY TO CALL FROM VB AND SEE THE RESULT. IF ITS WORKING FINE, GENERATE BATCH FILE USING VB.
BM KESHAV


0
 
usslindstromAuthor Commented:
Ah - sorry for not mentioning...   Yeah - the lines that I put in there "Attempting " & VariableCo....     they all work as advertised.

They're able to open up that text file and drop that line into it, with a timestamp Now() of when they ran.  But all I'm able to relay - is what I'm telling it to write to the file.  And unfortunately - not if PSExec was successful or not.

I've been shooting around a couple of ideas here, and may have to result to a batch file script solution that you mentioned.  It should be simple enough as I can definately export the contents of a query/table and then just call on the file that gets created.

If possible, I'd like to keep this as little footprint on the file system as possible though (even though I know a batch file wouldn't be more than 1 or 2 kb...) - but the future plans of this service in the network, are going to be removing the local access altogether, and preferably some .net asp stuff.  And even the text file outputs that I've been using are most likely going to get redirected in the long run.
0
 
usslindstromAuthor Commented:
Thank you everyone for your assistance here.  I ended up having to go with BMKE's solution of making it export the string to a batch file - then just calling the batch file from within VB - due to some extremely constrained time schedules that are surfacing.

I partially accepted CyberLex's answer - cause his code made wind of some other things I needed to take into consideration on my project.

On both counts, thank you VERY much for the assistance guys - I really appreciate it.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 5
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now