Does not work as well as the PS script, because of having to create subprocesses and routing stdout and stderr seems to be problematic.