CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Other Programming > Scripting - Client Side
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Scripting - Client Side Discuss client-side scripting issues. Client-side scripting such as JavaScript, JScript, and VBScript as well as technologies such as HTML and stylesheets.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 6th, 2009, 11:41 AM
    magicked magicked is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 3
    magicked is an unknown quantity at this point (<10)
    Writing wshell.exec to file cuts off

    I have a script set up to execute certain command line programs over the network with psexec and dump the output to separate files. Everything appears to run properly, but the output is cut off in each file. It seems like when reading the stdout stream, it can only buffer a limited amount of text. Here is the part of my script where I execute the command and write it to a file:

    Code:
    set objShell = CreateObject("WScript.Shell")
    set objFSO = CreateObject("Scripting.FileSystemObject")
    
    WScript.Echo "Checking installed software..."
    objFSO.CreateTextFile(strOutputDir & "installedsoftware.txt")
    '''''Open the output file'''''
    set objOutFile = objFSO.OpenTextFile(strOutputDir & "installedsoftware.txt", ForWriting)
    '''''Run the utility'''''
    set objExecObject = objShell.Exec(strExeDir & """" & strBaseDir & "\sysinternals\psinfo.exe"" -accepteula -s")
    
    Do While Not objExecObject.StdOut.AtEndOfStream
    	strText = objExecObject.StdOut.ReadLine()
    	objOutFile.writeLine(strText)
    Loop
    
    objOutFile.Write Now
    objOutFile.Close

    When I run the command (psinfo.exe in this case) in cmd, I get:

    Quote:
    System information for \\CAPC-D7D1B65542:
    Uptime: 1 day 0 hours 45 minutes 15 seconds
    Kernel version: Microsoft Windows XP, Uniprocessor Free
    Product type: Professional
    Product version: 5.1
    Service pack: 3
    Kernel build number: 2600
    Registered organization:
    Registered owner: CAPC
    Install date: 8/12/2009, 1:03:12 PM
    Activation status: Error reading status
    IE version: 8.0000
    System root: C:\WINDOWS
    Processors: 1
    Processor speed: 3.0 GHz
    Processor type: Intel(R) Xeon(TM) CPU
    Physical memory: 512 MB
    Video driver: VMware SVGA II
    Applications:
    ActiveState ActiveTcl 8.4.19.1 8.4.19.1
    HP USB Disk Storage Format Tool
    Hotfix for Windows XP (KB952287) 1
    Hotfix for Windows XP (KB970653-v3) 3
    Mozilla Firefox (3.5.3) 3.5.3 (en-US)
    OSSEC Hids Agent
    Security Update for Windows Internet Explorer 8 (KB971961) 1
    ecurity Update for Windows Internet Explorer 8 (KB972260) 1
    Security Update for Windows Internet Explorer 8 (KB974455) 1
    Security Update for Windows Media Player (KB952069)
    Security Update for Windows Media Player (KB954155)
    Security Update for Windows Media Player (KB968816)
    Security Update for Windows Media Player (KB973540)
    Security Update for Windows Media Player (KB973540)
    [...]
    Windows Genuine Advantage Notifications (KB905474) 1.9.0040.0
    Windows Internet Explorer 8 20090308.140743
    Windows XP Service Pack 3 20080414.031525
    Wireshark 1.2.1 1.2.1
    psinfo.exe exited on 192.168.1.202 with error code 3.
    However, when I run it via the script, the output text file only contains the following:

    Quote:
    System information for \\CAPC-D7D1B65542:
    Uptime: 1 day 0 hours 48 minutes 43 seconds
    Kernel version: Microsoft Windows XP, Uniprocessor Free
    Product type: Professional
    Product version: 5.1
    Service pack: 3
    Kernel build number: 2600
    Registered organization:
    Registered owner: CAPC
    Install date: 8/12/2009, 1:03:12 PM
    Activation status: Error reading status
    IE version: 8.0000
    System root: C:\WINDOWS
    Processors: 1
    Processor speed: 3.0 GHz
    Processor type: Intel(R) Xeon(TM) CPU
    Physical memory: 512 MB
    Video driver: VMware SVGA II
    Applications:
    ActiveState ActiveTcl 8.4.19.1 8.4.19.1
    HP USB Disk Storage Format Tool
    Hotfix for Windows XP (KB952287) 1
    Hotfix for Windows XP (KB970653-v3) 3
    Mozilla Firefox (3.5.3) 3.5.3 (en-US)
    OSSEC Hids Agent
    Security Update for Windows Internet Explorer 8 (KB971961) 1
    S
    11/6/2009 11:40:18 AM
    How do I get the rest of the output? Thanks for any help!

    Last edited by PeejAvery; November 6th, 2009 at 11:40 PM. Reason: Major code/quote tag cleanup.
    Reply With Quote
      #2    
    Old November 6th, 2009, 11:46 PM
    PeejAvery's Avatar
    PeejAvery PeejAvery is online now
    Super Moderator
    Power Poster
     
    Join Date: May 2002
    Location: United States
    Posts: 8,969
    PeejAvery has a brilliant future (2000+)PeejAvery has a brilliant future (2000+)PeejAvery has a brilliant future (2000+)PeejAvery has a brilliant future (2000+)PeejAvery has a brilliant future (2000+)PeejAvery has a brilliant future (2000+)PeejAvery has a brilliant future (2000+)PeejAvery has a brilliant future (2000+)PeejAvery has a brilliant future (2000+)PeejAvery has a brilliant future (2000+)PeejAvery has a brilliant future (2000+)
    Re: Writing wshell.exec to file cuts off

    You're making it rather hard on yourself. Why aren't you just reading the whole file rather than line by line?

    Code:
    Dim strFilePath, strFileContents
    strFilePath = "C:\"
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile(strFilePath, 1)
    strFileContents = objTextFile.ReadAll()
    
    Set objFSO = Nothing
    Set objTextFile = Nothing
    __________________
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.
    Reply With Quote
      #3    
    Old November 9th, 2009, 02:11 PM
    magicked magicked is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 3
    magicked is an unknown quantity at this point (<10)
    Re: Writing wshell.exec to file cuts off

    Quote:
    Originally Posted by PeejAvery View Post
    You're making it rather hard on yourself. Why aren't you just reading the whole file rather than line by line?

    Code:
    Dim strFilePath, strFileContents
    strFilePath = "C:\"
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile(strFilePath, 1)
    strFileContents = objTextFile.ReadAll()
    
    Set objFSO = Nothing
    Set objTextFile = Nothing
    Thanks for your help, but I'm not actually reading a file, although it appears ReadAll() also works on an executed object.

    I'm running a program that outputs to the command line, and I want to capture that output and put it in a text file. However, it can only capture a limited amount of the output before it reaches the end of the stream.

    Right now, here is where I call Exec() in the shell object and then grab the output:

    Code:
    [...]
    set objExecObject = objShell.Exec(strExeDir & """" & strBaseDir & "\sysinternals\psinfo.exe"" -accepteula -s")
    strText = objExecObject.StdOut.ReadAll()
    objOutFile.writeLine(strText)
    [...]
    When I open the resulting file, only part of the output is written. However, when I execute the exact same command in a shell myself, the entire output is printed on the screen. I have no idea where the rest of it is going.
    Reply With Quote
      #4    
    Old November 11th, 2009, 04:27 PM
    magicked magicked is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 3
    magicked is an unknown quantity at this point (<10)
    Re: Writing wshell.exec to file cuts off

    I discovered that Stdout only has 8,000 bytes of buffer space, which may have been my issue. Instead, I decided to use the Run() method. However, I ran into issues with this as well until I found out you could do the following:

    Code:
    [...]
    runCmd = strExeDir & """" & strBaseDir & "\sysinternals\psinfo.exe"" -accepteula -s > """ & strOutputDir & "installedsoftware.txt"""
    return = objShell.Run("cmd /c """ & runCmd & """",1,true)
    [...]
    This allowed me to run psinfo.exe on a remote system with psexec and save the resulting output to a text file on my local system.

    And just for further information, the full command being run was:

    Code:
    "C:\Documents and Settings\magicked\Desktop\remote\sysinternals\psexec.exe" \\192.168.13.37 -u Administrator -p Password -c "C:\Documents and Settings\magicked\Desktop\remote\sysinternals\psinfo.exe" -accepteula -s > "C:\Documents and Settings\magicked\Desktop\remote\output\192.168.13.37\installedsoftware.txt"
    (Don't worry, I scrubbed it. Password is not my password. )

    I ran into trouble with Run(), because I didn't realize I had to put quotes around the whole runCmd. The script would ignore all my quotes around each path in the command otherwise, and it would error out with:

    Code:
    'C:\Documents' is not recognized as an internal or external command, operable program or batch file.
    When I added the quotes around the entire command, it worked properly!
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Other Programming > Scripting - Client Side


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 09:49 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009