how to use wps in delphi 7
WPS (Windows PowerShell Script) is a scripting language developed by Microsoft for automating administrative tasks in Windows. To use WPS in Delphi 7, you need to follow these steps:
-
Download and install the Windows PowerShell SDK from Microsoft's website.
-
Open Delphi 7 and create a new project.
-
Add a new unit to the project and name it "WPSUnit".
-
In the WPSUnit, add the following code:
unit WPSUnit;
interface
uses SysUtils, Classes, Variants, WPS_TLB;
type TWPS = class(TObject) private FPowerShell: IWindowsPowerShell; public constructor Create; destructor Destroy; override; procedure ExecuteScript(const AScript: string); end;
implementation
constructor TWPS.Create; begin inherited; FPowerShell := CoWindowsPowerShell.Create; end;
destructor TWPS.Destroy; begin FPowerShell := nil; inherited; end;
procedure TWPS.ExecuteScript(const AScript: string); var LCommand: ICommand; LResults: IEnumVariant; LResult: OleVariant; LBuffer: string; begin LCommand := FPowerShell.CreateCommand; LCommand.AddScript(AScript); LResults := LCommand.Invoke; while LResults.Next(1, LResult, nil) = S_OK do begin LBuffer := LBuffer + LResult + #13#10; LResult := Unassigned; end; ShowMessage(LBuffer); end;
end.
-
Save the unit and add it to the project.
-
In the main form, add a button and name it "btnExecute".
-
Double-click on the button to create an event handler.
-
In the event handler, add the following code:
procedure TForm1.btnExecuteClick(Sender: TObject); var LPS: TWPS; begin LPS := TWPS.Create; try LPS.ExecuteScript('Get-Process | Select-Object Name,Id'); finally LPS.Free; end; end;
-
Save and run the project.
-
Click on the "btnExecute" button to execute the WPS script and display the results in a message box.
Note: This is just a basic example of how to use WPS in Delphi 7. You can modify the code to suit your needs and execute more complex scripts
原文地址: https://www.cveoy.top/t/topic/dHVM 著作权归作者所有。请勿转载和采集!