Recent

Author Topic: run command Powershell  (Read 3673 times)

Ecsa

  • New Member
  • *
  • Posts: 13
run command Powershell
« on: February 10, 2022, 06:07:31 pm »
Hello everyone, I'm trying to execute the entire command line that you will see below via powershell.

Quote
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

Using this code but I don't have any favorable results.

Code: Pascal  [Select][+][-]
  1. ChocoPS:= TProcess.Create(Form2);
  2. ChocoPS.Options:=[poUsePipes,poWaitOnExit,poNoConsole,poStderrToOutPut];
  3. ChocoPS.Executable:='PowerShell.exe';
  4. ChocoPS.Parameters.Add('Set-ExecutionPolicy Bypass -Scope Process -Force;');
  5. ChocoPS.Parameters.Add('[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072;');
  6. ChocoPS.Parameters.Add('iex ((New-Object System.Net.WebClient).DownloadString("https://community.chocolatey.org/install.ps1"))');
  7. ChocoPS.Active:=True;
  8. Memo1.Lines.LoadFromStream(ChocoPS.Output);
  9. ChocoPS.Free;
         

Is there a way to execute the commands in question?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11524
  • FPC developer.
Re: run command Powershell
« Reply #1 on: February 10, 2022, 10:58:50 pm »
Find the syntax how this would execute on the commandline with explicite a powershell invocation.

Then convert that to tprocess code with a parameters.add for each word (space separated), except for quoted parts, pass them as a single parameters.add without quotes.

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: run command Powershell
« Reply #2 on: February 11, 2022, 01:37:26 am »

Does this work?
Code: Pascal  [Select][+][-]
  1.  
  2.  function  system(s:pchar):integer ; cdecl external 'msvcrt.dll' name 'system';
  3.  
  4.  var
  5.  s:ansistring='PowerShell.exe'+#13#10+'Set-ExecutionPolicy Bypass -Scope Process -Force;'+#13#10+'[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072;'+#13#10+'iex ((New-Object System.Net.WebClient).DownloadString("https://community.chocolatey.org/install.ps1"))';
  6.  
  7.  begin
  8.  writeln(s);
  9.  writeln;
  10.  system(pchar(s));
  11. writeln('Done');
  12.  readln;
  13.  end.
  14.  

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: run command Powershell
« Reply #3 on: February 11, 2022, 03:14:33 am »
Hello,
Hello everyone, I'm trying to execute the entire command line that you will see below via powershell.

Quote
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

Using this code but I don't have any favorable results.

Code: Pascal  [Select][+][-]
  1. ChocoPS:= TProcess.Create(Form2);
  2. ChocoPS.Options:=[poUsePipes,poWaitOnExit,poNoConsole,poStderrToOutPut];
  3. ChocoPS.Executable:='PowerShell.exe';
  4. ChocoPS.Parameters.Add('Set-ExecutionPolicy Bypass -Scope Process -Force;');
  5. ChocoPS.Parameters.Add('[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072;');
  6. ChocoPS.Parameters.Add('iex ((New-Object System.Net.WebClient).DownloadString("https://community.chocolatey.org/install.ps1"))');
  7. ChocoPS.Active:=True;
  8. Memo1.Lines.LoadFromStream(ChocoPS.Output);
  9. ChocoPS.Free;
         

Is there a way to execute the commands in question?
in the DownloadString Method replace double quote with single quote like this :
Code: Pascal  [Select][+][-]
  1. ChocoPS.Parameters.Add('iex ((New-Object System.Net.WebClient).DownloadString(''https://community.chocolatey.org/install.ps1''))');

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

badmintonfan

  • New Member
  • *
  • Posts: 49
Re: run command Powershell
« Reply #4 on: December 24, 2023, 02:40:46 pm »
I tested it within windows 10 and powershell 5.1,and found only few cmdlet can run,I have tested get-process also,but thers is no result
procedure TForm1.Button1Click(Sender: TObject);
var ChocoPS : TProcess;
  oput: ansistring;
begin
    ChocoPS:= TProcess.Create(Form1);
    ChocoPS.Options:=[poUsePipes,poWaitOnExit,poNoConsole,poStderrToOutPut];
    ChocoPS.Executable:='PowerShell.exe';
    //ChocoPS.Parameters.Add('Get-WmiObject -Class Win32_BIOS -ComputerName .');
    //ChocoPS.Parameters.Add('whoami /upn');
    ChocoPS.Parameters.Add('Get-NetAdapter');
    ChocoPS.Active:=True;
    //oput:=ChocoPS.Output;
    Memo1.Lines.LoadFromStream(ChocoPS.Output,Tencoding.ANSI);
    //memo1.Append(oput);
    ChocoPS.Free;         

TRon

  • Hero Member
  • *****
  • Posts: 2675
Re: run command Powershell
« Reply #5 on: December 24, 2023, 03:03:27 pm »
I tested it within windows 10 and powershell 5.1,and found only few cmdlet can run,I have tested get-process also,but thers is no result
I don't know how powershell expects its parameters but it might be that you are requited to add each individual parameter to the process options (it might even depend on the subcommand being executed by powershell).

e.g f.e.:
Code: Pascal  [Select][+][-]
  1.  ChocoPS.Parameters.Add('Get-WmiObject');
  2.  ChocoPS.Parameters.Add('-Class Win32_BIOS');
  3.  ChocoPS.Parameters.Add('--ComputerName .');
  4.  
or perhaps even:
Code: Pascal  [Select][+][-]
  1.  ChocoPS.Parameters.Add('Get-WmiObject');
  2.  ChocoPS.Parameters.Add('-Class');
  3.  ChocoPS.Parameters.Add('Win32_BIOS');
  4.  ChocoPS.Parameters.Add('--ComputerName');
  5.  ChocoPS.Parameters.Add('.');
  6.  
I also wonder how powershell treats the dot in relation to expansion.

Also your example seem to want to provide multiple commands at once (guessing there as you commented them) which would probably require other additional powershell separator characters to distinguish between commands.

I would suggest to start experimenting with simple commands, adding some optons and work your way up until you figure out how the parameters are expected to be provided at tprocess for powershell.

Sorry, no powershell here at my end in order to check/test.

badmintonfan

  • New Member
  • *
  • Posts: 49
Re: run command Powershell
« Reply #6 on: December 25, 2023, 07:51:54 am »
These commented which are have been tested and got result

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: run command Powershell
« Reply #7 on: December 25, 2023, 06:34:09 pm »
Hello,
I tested it within windows 10 and powershell 5.1,and found only few cmdlet can run,I have tested get-process also,but thers is no result
With this code :
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   Pwsh: IPwsh;
  4.   Resultat: WideString;
  5. begin
  6.   CreatePwshInstance(Pwsh);
  7.   Resultat := Pwsh.ExecuteCmd('(Get-Process *) | Out-String');
  8.   Memo1.Append(Resultat);
  9. end;  
  10.  
i get what you can see in attachment.

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

badmintonfan

  • New Member
  • *
  • Posts: 49
Re: run command Powershell
« Reply #8 on: December 26, 2023, 12:27:15 pm »
Hello Jurassic
Could you please explain me IPwsh
Hello,
I tested it within windows 10 and powershell 5.1,and found only few cmdlet can run,I have tested get-process also,but thers is no result
With this code :
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   Pwsh: IPwsh;
  4.   Resultat: WideString;
  5. begin
  6.   CreatePwshInstance(Pwsh);
  7.   Resultat := Pwsh.ExecuteCmd('(Get-Process *) | Out-String');
  8.   Memo1.Append(Resultat);
  9. end;  
  10.  
i get what you can see in attachment.

Friendly, J.P

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: run command Powershell
« Reply #9 on: December 26, 2023, 01:59:07 pm »
Hello,
IPwsh is an Interface with an external dll
Code: Pascal  [Select][+][-]
  1. type
  2.   IPwsh = interface(IUnknown)
  3.    ['{8871C5E0-B296-4AB8-AEE7-F2553BACB735}']
  4.    function ExecuteCmd(const Cmd: WideString): WideString; safecall;
  5.    end;
  6. {$IFDEF WIN32}
  7.    procedure CreatePwshInstance(out Sample: IPwsh); stdcall; external 'LazPwshJP32.dll';
  8. {$ENDIF}
  9. {$IFDEF WIN64}
  10.    procedure CreatePwshInstance(out Sample: IPwsh); stdcall; external 'LazPwshJP64.dll';
  11. {$ENDIF}
     
The LazPwshJP dlls are unmanaged export Csharp dll using the PowerShell Csharp class. The dotnet framework used is the 4.0 (included in windows, sometimes must be actived). The dlls have MIT licenses.
In Attachment a lazarus demo program with the dlls.
tested on windows 11 with Lazarus 2.2.6  64 bits and PowerShell 5.1.
Friendly, J.P
« Last Edit: December 26, 2023, 02:20:15 pm by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

badmintonfan

  • New Member
  • *
  • Posts: 49
Re: run command Powershell
« Reply #10 on: December 26, 2023, 03:41:37 pm »
Hello Jurassic Pork
That is what I wanted,thank you very much

badmintonfan

  • New Member
  • *
  • Posts: 49
Re: run command Powershell
« Reply #11 on: January 10, 2024, 08:38:24 am »
Hello,
IPwsh is an Interface with an external dll

The LazPwshJP dlls are unmanaged export Csharp dll using the PowerShell Csharp class. The dotnet framework used is the 4.0 (included in windows, sometimes must be actived). The dlls have MIT licenses.
In Attachment a lazarus demo program with the dlls.
tested on windows 11 with Lazarus 2.2.6  64 bits and PowerShell 5.1.
Friendly, J.P
Hello
How to process the prompt during execute a cmdlet

 

TinyPortal © 2005-2018