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.
var
s : String;
SLO: TStringList;
begin
PZ.Executable := 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe';
PZ.Tag := 1; // Process is running
SLO.Clear;
s := 'Enter-PSSession ' + CompName + LineEnding; // CompName is the computer name and LineEnding is CRLF
PZ.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;
...
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