Uniface User Forum

 View Only
  • 1.  Capture Powershell output using CommandOut

    Posted 02-07-2023 16:28
    While it seems pretty simple to capture the output of a Windows command using the CommandOut method of the OS signature, it appears that it not possible to capture the output of Powershell commands such as Write-Host or Write-Output, without first sending the output to a text file and then using a Windows command to read, display and capture the output via CommandOut in Uniface.

    Has anyone resolved this issue and managed to capture Powershell output directly and how?

    ------------------------------
    Harjit Batra
    IT Director
    Rocket Forum Shared Account
    ------------------------------


  • 2.  RE: Capture Powershell output using CommandOut

    Posted 02-08-2023 08:45
    This may be an option:- 

    It is a bit chopped up, but the idea is there....  It takes the output from a dos command (git current branch).
    Compares the output and returns a variable errorlevel.
    Uniface then gets this.

    ignore the sPct and sX... They are just problems I had with %age in uniface.

    sPct = "%%""
    sX = "%x%"
    ;;	Is it already the correct branch errorlevel=9(already on branch)
    putitem sScriptPut, -1, "for /f %%"usebackq tokens=*%%" %% i in (`%%"%%$SGIT$%%%%%" rev-parse --abbrev-ref HEAD`) DO set x=%% i"
    putitem sScriptPut, -1, "IF %%sPct%%%%%sX%%%%%sPct%%% == %%"%%pBranch%%%%%" Set errorlevel=9"
    putitem sScriptPut, -1, "echo HERE=%ERRORLEVEL%"
    ...
    ...
    activate "OS".COMMANDOUT("{filename}", sOutput)
    scan sOutput, "HERE="
    if ($status = 0)
    	nStart = $result + 5
    	nError  = $number(sOutput[nStart:4])
    endif​


    ------------------------------
    Osman Shariff
    ------------------------------



  • 3.  RE: Capture Powershell output using CommandOut

    Posted 02-15-2023 09:05

    I don't think my original question was understood. I am looking to capture the output of a Powershell command or script via the commandout method. I already know how to capture the output of a Windows Shell command - commands like dir, type, sort etc send their output to stdout and the ComandOut has no issue grabbing the output sent to stdout from these commands. However when you attempt to run Powershell.exe directly or under the control of a "cmd /c", any output created by the Write-Host or Write-Output from within the Powershell session does not appear to be captured by the CommandOut method and THAT is the issue I am trying to resolve.

    Actually, it appears that trying to run Powershell directly (i.e. without the cmd /c start /wait /b /min "" prefix) does not even run either Powershell or the command or script file that follows, however that is a separate problem in itself.



    ------------------------------
    Harjit Batra
    IT Director
    Rohrer Corporation
    ------------------------------



  • 4.  RE: Capture Powershell output using CommandOut

    Posted 02-18-2023 05:20
    Edited by Gianni Sandigliano 02-18-2023 06:29

    Hi Harjit,

    I agree with you about the issue with powershell;
    anyhow as a temporary solution you could pipe your command to redirect the output to a file and reload from that file.

    Regards,
    Gianni



    ------------------------------
    Gianni Sandigliano
    IT
    ------------------------------



  • 5.  RE: Capture Powershell output using CommandOut

    PARTNER
    Posted 02-20-2023 07:08

    Is it a case of requiring channel redirection. 

    A powershell script, to run your powershell script  which has something like

    ./yourscript.ps *>&1

    Which should put all channels to the output channel, and make them 'capturable'? 

    Iain



    ------------------------------
    Iain Sharp
    Head of Technical Services
    Pci Systems Ltd
    Sheffield GB
    ------------------------------



  • 6.  RE: Capture Powershell output using CommandOut

    PARTNER
    Posted 02-20-2023 07:11

    Except I notice you've mentioned not being able to capture Write-Output either.... 



    ------------------------------
    Iain Sharp
    Head of Technical Services
    Pci Systems Ltd
    Sheffield GB
    ------------------------------