B4J Question [SOLVED] Can't execute/return output from Powershell

valentino s

Active Member
Licensed User
Longtime User
Solved, see n. 3


Hi all,
I want to get output from powershell. I use the Erel's snippet.
It arrives to log "ok3", but never to "ok4".

In CLI powershell -Command Get-Command -name *Get-app* works.

Any idea about my error ?

Here the code

Code:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
 
End Sub

Sub AppStart (Args() As String)
    Log("Hello world!!!")
    Dim lista As String
    PowerShellScript("Get-Command -name *Get-app*")
    Log(lista)
End Sub



Public Sub PowerShellScript(s As String) As ResumableSub
    Log("OK2")

    s = s.Replace(CRLF, ";").Replace("""", "'")
    Dim shl As Shell
    shl.InitializeDoNotHandleQuotes("shl", "powershell.exe", Array("-Command", s))
    shl.Run(-1)
    Log("OK3")
    Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    Log("OK4")

    Dim res As ShellSyncResult
    res.ExitCode = ExitCode
    res.StdErr = StdErr
    res.StdOut = StdOut
    res.Success = Success
    If StdErr <> "" Then
        Log(StdErr)
        If ExitCode = 0 Then res.ExitCode = 1
    End If
    Log("OK5")
    Log (res)
    Return res
End Sub
 

Attachments

  • powershelltest.zip
    1.1 KB · Views: 142
Last edited:

valentino s

Active Member
Licensed User
Longtime User
All you need to do is tell the application where the output should go:
B4X:
    PowerShellScript("Get-Command -name *Get-app* > e:\_temp\test_output.txt")
Note: replace the path with your situation.
First of all, thanks !
The output to a file works. It's created with the datas I need.
I thought it can works without creating a file. ... and indeed, it works without a temp .txt file, read later.

However the script does not arrive to OK4; "Wait For shl_ProcessCompleted " doesn't proceed.
I'm looking for a solution about the waiting function.

SOLVED:
StartMessageLoop 'need to call this as this is a console app.

b4j:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Private shl As Shell
End Sub

Sub AppStart (Args() As String)
    Log("Hello world!!!")
    Dim lista As String
    'PowerShellScript("Get-Command -name *Get-app* > c:\temp\test_output.txt")
    PowerShellScript("Get-Command -name *Get-app*")
    Log(lista)
End Sub



Public Sub PowerShellScript(s As String) As ResumableSub
    Log("OK2")

    s = s.Replace(CRLF, ";").Replace("""", "'")
    Dim shl As Shell
    shl.InitializeDoNotHandleQuotes("shl", "powershell.exe", Array("-Command", s))
    shl.Run(-1)
    StartMessageLoop 'need to call this as this is a console app.

End Sub

Sub shl_ProcessCompleted(Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    If Success And ExitCode = 0 Then
        Log("Success")
        Log(StdOut)
    Else
        Log("Error: " & StdErr)
    End If
    ExitApplication
End Sub
 
Last edited:
Upvote 0
Top