TAsyncProcess and Powershell lazarus / 34-Windows ---------------------------------------------------------------------- HomeBoy38 · 2024-02-22 11:04 Hello, I have an issue with my code and I cannot figure out what is the issue, certainly something I do not do correctly... I have a (visual component) TAsyncProcess called PZ with [poNoConsole, poStderrToOutPut, poUsePipes]. The TAsyncProcess has 'OnReadData' event. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var s : String; SLO: TStringList; beginPZ.Executable := 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe';PZ.Tag := 1; // Process is runningSLO.Clear;s := 'Enter-PSSession ' + CompName + LineEnding; // CompName is the computer name and LineEnding is CRLFPZ.Input.Write(s[1], Length(s));while PZ.Tag <> 0 do  Application.HandleMessage;... s := 'Invoke-Command {Get-WmiObject Powershell Command}' + LineEnding; PZ.Tag := 1; PZ.Input.Write(s[1], Length(s)); while PZ.Tag <> 0 do  Application.HandleMessage;...  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMainForm.PZReadData(Sender: TObject);var J: Integer; SLOtmp: TStringList;begin J := 0; SLOtmp := TStringList.Create; SLOtmp.LoadFromStream(PZ.Output);  if J < SLOtmp.Count then begin  for J := J to SLOtmp.Count - 1 do   SLO.Add(SLOtmp.Strings[J]); end;  if SLOtmp.Count > 0 then begin  if ((Copy(SLOtmp.Strings[SLOtmp.Count - 1], 1, 3) = 'PS ') and (Copy(SLOtmp.Strings[SLOtmp.Count - 1], Length(SLOtmp.Strings[SLOtmp.Count - 1]) - 1, 1) = '>')) or     ((Copy(SLOtmp.Strings[SLOtmp.Count - 1], 1, Length(FSettings.ECompName.Text) + 7) = '[' + FSettings.ECompName.Text + ']: PS ') and (Copy(SLOtmp.Strings[SLOtmp.Count - 1], Length(SLOtmp.Strings[SLOtmp.Count - 1]) - 1, 1) = '>')) then   PZ.Tag := 0; end;  if Assigned(SLOtmp) then SLOtmp.Free;end;  The goal is to enter the remote powershell of another computer and get the result of each command I should run. I previously used remote WMI commands (wmic.exe) but it is getting obsolete, I then used remote PowerShell commands (Get-CimInstance -computername) which are running correctly. The PZReadData is something I found (maybe on this forum), I only added the prompt detection to exit. The issue : The issue is, according to me, related to my PZReadData procedure. The procedure works 99% of the time, but when it fails, I may be falling in my while loop. What I can say, is that the PowerShell commands are sometimes returning an unexpected result (missing result, broken format result and something I am not able to catch that fails my procedure). I tried to log everything, I tried to put some timeouts, to workaround the issue, but each time it fails, I loop and the workaround is not working, so clearly, I am not handling the process correctly. One workaround I found is the LazPwshJP64.dll in the topic,58276.0.html but I would like to understand what I am doing wrong and what I should do right, avoid using this additionnal DLL, avoid a blackbox dll I use blindly without understanding it. Thanks ---------------------------------------------------------------------- Thaddy · 2024-02-22 15:47 This is again an example that the stream source needs to be flushed before you can read that stream. ---------------------------------------------------------------------- HomeBoy38 · 2024-02-22 17:21 ok, I got the idea but can you tell me what/where/how I should do it? I have found threads saying it is not necessary, some say to "closeoutput" (but I am afraid I will hang on the next command), ... so I am a bit lost Note: I am currently testing this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i PZ.Tag := 1;PZ.Input.Write(s[1], Length(s));FileFlush(PZ.Output.Handle);        // Flush testwhile PZ.Tag <> 0 do Application.HandleMessage; Nope, after a few hours, loop of the death, either the flush try is not what was suggested, either the truth is out there