Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How do I redirect output of my console using PsExec?

How do I redirect output of my console using PsExec?

Scheduled Pinned Locked Moved C / C++ / MFC
questiontestingbeta-testingtools
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SherTeks
    wrote on last edited by
    #1

    I'm using a remote execution tool called 'PsExec' from sysinternals. I use it this way in my command prompt : D:\Tools\Testing>psexec netsh firewall show opmode>D:\Log.txt This generates the output for 'netsh firewall show opmode' and redirects to D:\Log.txt Now, I implement the same logic/code in my application like this :

    CString csCmdLineParams = "netsh firewall show opmode>D:\\TestdwLog.txt";

    STARTUPINFO StartUpInfo;
    PROCESS_INFORMATION ProcInfo;

    DWORD ExitCode = -1;

    memset(&StartUpInfo, 0, sizeof(StartUpInfo));

    memset(&ProcInfo, 0, sizeof(ProcInfo));

    StartUpInfo.dwFlags = STARTF_USESHOWWINDOW;

    StartUpInfo.wShowWindow = SW_SHOW;

    CreateProcess( "C:\\windows\\system32\\cmd.exe", csCmdLineParams.GetBuffer(csCmdLineParams.GetLength() + ONE), NULL, NULL, NULL, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &StartUpInfo, &ProcInfo);

    WaitForSingleObject(ProcInfo.hProcess, INFINITE);

    GetExitCodeProcess(ProcInfo.hProcess, &ExitCode );

    if (ExitCode) {
    csCmdLineParams.ReleaseBuffer();
    CloseHandle(ProcInfo.hProcess);
    }

    csCmdLineParams.ReleaseBuffer();

    CloseHandle(ProcInfo.hProcess);

    But, I'm not able to redirect the output to D:\TestdwLog.txt. In fact the file is not created itself. Note : I do have administrator rights for my system. [required for PsExec] What am I doing wrongly ? I also tried StartUpInfo_IDI.dwFlags = STARTF_USESTDHANDLES; instead of StartUpInfo.dwFlags = STARTF_USESHOWWINDOW; Replies would be appreciated Thanks in advance.

    G D 2 Replies Last reply
    0
    • S SherTeks

      I'm using a remote execution tool called 'PsExec' from sysinternals. I use it this way in my command prompt : D:\Tools\Testing>psexec netsh firewall show opmode>D:\Log.txt This generates the output for 'netsh firewall show opmode' and redirects to D:\Log.txt Now, I implement the same logic/code in my application like this :

      CString csCmdLineParams = "netsh firewall show opmode>D:\\TestdwLog.txt";

      STARTUPINFO StartUpInfo;
      PROCESS_INFORMATION ProcInfo;

      DWORD ExitCode = -1;

      memset(&StartUpInfo, 0, sizeof(StartUpInfo));

      memset(&ProcInfo, 0, sizeof(ProcInfo));

      StartUpInfo.dwFlags = STARTF_USESHOWWINDOW;

      StartUpInfo.wShowWindow = SW_SHOW;

      CreateProcess( "C:\\windows\\system32\\cmd.exe", csCmdLineParams.GetBuffer(csCmdLineParams.GetLength() + ONE), NULL, NULL, NULL, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &StartUpInfo, &ProcInfo);

      WaitForSingleObject(ProcInfo.hProcess, INFINITE);

      GetExitCodeProcess(ProcInfo.hProcess, &ExitCode );

      if (ExitCode) {
      csCmdLineParams.ReleaseBuffer();
      CloseHandle(ProcInfo.hProcess);
      }

      csCmdLineParams.ReleaseBuffer();

      CloseHandle(ProcInfo.hProcess);

      But, I'm not able to redirect the output to D:\TestdwLog.txt. In fact the file is not created itself. Note : I do have administrator rights for my system. [required for PsExec] What am I doing wrongly ? I also tried StartUpInfo_IDI.dwFlags = STARTF_USESTDHANDLES; instead of StartUpInfo.dwFlags = STARTF_USESHOWWINDOW; Replies would be appreciated Thanks in advance.

      G Offline
      G Offline
      Garth J Lancaster
      wrote on last edited by
      #2

      Im pretty sure you'll get mileage out of the following links - the 3rd link is for VB, but illustrates an alternate technique - ymmv http://www.codeproject.com/KB/threads/RTconsole.aspx[^] http://www.codeproject.com/KB/threads/redir.aspx[^] http://www.devx.com/vb2themax/Article/19825/1954[^] Im pretty sure search on CP or google would have found these for you..

      1 Reply Last reply
      0
      • S SherTeks

        I'm using a remote execution tool called 'PsExec' from sysinternals. I use it this way in my command prompt : D:\Tools\Testing>psexec netsh firewall show opmode>D:\Log.txt This generates the output for 'netsh firewall show opmode' and redirects to D:\Log.txt Now, I implement the same logic/code in my application like this :

        CString csCmdLineParams = "netsh firewall show opmode>D:\\TestdwLog.txt";

        STARTUPINFO StartUpInfo;
        PROCESS_INFORMATION ProcInfo;

        DWORD ExitCode = -1;

        memset(&StartUpInfo, 0, sizeof(StartUpInfo));

        memset(&ProcInfo, 0, sizeof(ProcInfo));

        StartUpInfo.dwFlags = STARTF_USESHOWWINDOW;

        StartUpInfo.wShowWindow = SW_SHOW;

        CreateProcess( "C:\\windows\\system32\\cmd.exe", csCmdLineParams.GetBuffer(csCmdLineParams.GetLength() + ONE), NULL, NULL, NULL, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &StartUpInfo, &ProcInfo);

        WaitForSingleObject(ProcInfo.hProcess, INFINITE);

        GetExitCodeProcess(ProcInfo.hProcess, &ExitCode );

        if (ExitCode) {
        csCmdLineParams.ReleaseBuffer();
        CloseHandle(ProcInfo.hProcess);
        }

        csCmdLineParams.ReleaseBuffer();

        CloseHandle(ProcInfo.hProcess);

        But, I'm not able to redirect the output to D:\TestdwLog.txt. In fact the file is not created itself. Note : I do have administrator rights for my system. [required for PsExec] What am I doing wrongly ? I also tried StartUpInfo_IDI.dwFlags = STARTF_USESTDHANDLES; instead of StartUpInfo.dwFlags = STARTF_USESHOWWINDOW; Replies would be appreciated Thanks in advance.

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Put psexec netsh firewall show opmode>D:\Log.txt in a batch file, and run the batch file from CreateProcess().

        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        S 1 Reply Last reply
        0
        • D David Crow

          Put psexec netsh firewall show opmode>D:\Log.txt in a batch file, and run the batch file from CreateProcess().

          "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          S Offline
          S Offline
          SherTeks
          wrote on last edited by
          #4

          Thanks David. This works. Also found another option of using system() Thanks

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups