在编写 PowerShell 脚本时,有时需要以管理员权限运行以执行特定的操作。本文将介绍一种方法,通过以管理员身份启动新的 PowerShell 进程来实现这一目的。
应用场景,使用delphi中的tdoscommand组件运行第一个ps1脚本,报权限错误。使用“runasadmin.ps1”来自动调用powershell ise 工具运行。
一、delphi调用ps1的主程序如下:
"C:\delphisource\createshared\Project1.dproj"
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls, DosCommand;
type
TForm1 = class(TForm)
Button1: TButton;
DosCommand1: TDosCommand;
Memo1: TMemo;
MemoCommandLine: TMemo;
procedure DosCommand1NewLine(ASender: TObject; const ANewLine: string; AOutputType: TOutputType);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
commandstr:string

文章介绍了如何在Delphi应用中通过TDosCommand组件调用PowerShell脚本,并以管理员权限执行,包括使用`runasadmin.ps1`脚本来启动PowerShell进程并处理权限问题。
最低0.47元/天 解锁文章
1830

被折叠的 条评论
为什么被折叠?



