There might be some native dll calls wrappers in julia, but in this case I would just use run and pipelines to get what you want.
I got a bunch of red text and a thrown error from run when I initially did:
run(pipeline(`powershell -c 'get-process d* -fileversioninfo'`, stdout="output.txt"))
So I suppressed the errors and now I get this:
julia> run(pipeline(Cmd(`powershell -c 'get-process d* -fileversioninfo -ErrorAction Ignore'`, ignorestatus=true), stdout="output.txt"))
Process(`powershell -c 'get-process d* -fileversioninfo -ErrorAction Ignore'`, ProcessExited(1))
julia> println(read("output.txt", String))
ProductVersion FileVersion FileName
-------------- ----------- --------
10.0.22621.1 10.0.22621.1 ... C:\Windows\System32\DataExchangeHost.exe
10.0.22621.1 10.0.22621.1 ... C:\WINDOWS\system32\DllHost.exe
10.0.22621.1 10.0.22621.1 ... C:\WINDOWS\system32\DllHost.exe
10.0.22621.1 10.0.22621.1 ... C:\WINDOWS\system32\DllHost.exe
10.0.22621.1 10.0.22621.1 ... C:\WINDOWS\system32\DllHost.exe
10.0.22621.1 10.0.22621.1 ... C:\WINDOWS\system32\DllHost.exe
10.0.22621.1 10.0.22621.1 ... C:\WINDOWS\system32\DllHost.exe
Tested on Windows 11 with Julia 1.9.2.
See the documentation on run
and pipeline
and Cmd
here:
https://docs.julialang.org/en/v1/manual/running-external-programs/
I like this function, too, but it does need to change AbstractCmd
to support a pipeline inside.
https://discourse.julialang.org/t/collecting-all-output-from-shell-commands/15592/7
function communicate(cmd::Base.AbstractCmd, input="") # minor changes to args
inp = Pipe()
out = Pipe()
err = Pipe()
process = run(pipeline(cmd, stdin=inp, stdout=out, stderr=err), wait=false)
close(out.in)
close(err.in)
stdout = @async String(read(out))
stderr = @async String(read(err))
write(process, input)
close(inp)
wait(process)
# look at all exitcodes
if process isa Base.ProcessChain
exitcode = maximum([p.exitcode for p in process.processes])
else
exitcode = process.exitcode
end
return (
stdout = fetch(stdout),
stderr = fetch(stderr),
code = process.exitcode
)
end
@show communicate(`cat`, "hello")
@show communicate(`sh -c "cat; echo errrr 1>&2; exit 3"`, "hello")