When I try to run the code below in powershell, everything goes smooth and I get the output that I want.
Start-Transcript -Path log.txt
V:\neo4j\relate-data\dbmss\dbms-a5914a53-3b7d-42d0-a050-8bd6058e5e98\bin\neo4j-admin dump --database=neo4j --to=V:\neo4j\dumps
Stop-Transcript
However, the log.txt file only has its header and the transcript itself is empty with no output? It's like start/stop transcript doesn't capture anything. It works though if i use ex. "dir" as command.
CodePudding user response:
The problem is that the neo4j-admin
directly writes to the console, circumventing any attempts to redirect its output, so Start-Transcript
can't do its job.
If the tool can't be changed, then there is a workaround. Recent Windows versions come with a pseudo-console (ConPTY) API, that provides a virtual console whose output can be captured programmatically. Using this API isn't trivial and seems a bit overkill for this task. Anyway, here is some C# sample code that could be ported to PowerShell or directly used as an assembly.
A simpler way is to use mintty
, a console emulator that is able to log console output (it propably uses ConPTY internally). If you have Git for Windows installed, its path typically is c:\Program Files\Git\usr\bin\mintty.exe
. Otherwise you can get mintty
from GitHub.
Here is a simplified sample:
WriteConsole.ps1:
[Console]::WriteLine('foobar')
Log all output of "WriteConsole.ps1", even direct writes to the console:
& 'c:\Program Files\Git\usr\bin\mintty.exe' -w hide -l log.txt powershell WriteConsole.ps1
See mintty
command-line parameters.