以管理员权限运行 PowerShell 脚本的方法

在编写 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;
  PowerShellPath, ScriptPath, CommandLine: string;
begin

  for i := 0 to MemoCommandLine.Lines.Count-1 do
    begin
      commandstr:=self.MemoCommandLine.Lines[i] +' && '+commandstr;
    end;
      PowerShellPath := 'powershell.exe '; // 假设 PowerShell 可执行文件在系统环境变量中
#39 + '}';

  CommandLine := 'C:\delphisource\createshared\runasadmin.ps1';
  DosCommand1.CommandLine := PowerShellPath + ' -ExecutionPolicy Bypass -File ' + CommandLine;
  DosCommand1.Execute;
end;

procedure TForm1.DosCommand1NewLine(ASender: TObject; const ANewLine: string; AOutputType: TOutputType);
begin
  memo1.Lines.Add(ANewLine);
end;

end.

首先,我们需要定义第一个要执行的脚本文件的路径。假设脚本文件的路径是"C:\delphisource\createshared\CreateShareFolder.ps1"。

二、第一段ps1代码如下:

$shareName = "SharedFolder"
$folderPath = "C:\delphicode\win32"
$shareDescription = "Shared folder description"
$readAccess = "Everyone"
$writeAccess = "Administrators"

New-SmbShare -Name $shareName -Path $folderPath -Description $shareDescription -FullAccess $readAccess -ChangeAccess $writeAccess

接下来,我们将创建启动进程所需的参数。我们使用`System.Diagnostics.ProcessStartInfo`对象来指定要启动的进程的信息。我们设置文件名为`powershell.exe`,表示要启动的进程是 PowerShell。然后,我们将参数设置为`-NoProfile -ExecutionPolicy Bypass -File "$scriptPath"`。`-NoProfile`参数表示不加载用户配置文件,`-ExecutionPolicy Bypass`参数绕过执行策略,允许执行任何脚本,`-File "$scriptPath"`参数指定要运行的脚本文件的路径。最后,我们将动词设置为`RunAs`,表示以管理员身份运行进程。

"C:\delphisource\createshared\runasadmin.ps1"

三、第二段ps1代码如下(使用administrator权限运行):

$scriptPath = "C:\delphisource\createshared\CreateShareFolder.ps1"

# 创建启动进程的参数
$processStartInfo = New-Object System.Diagnostics.ProcessStartInfo
$processStartInfo.FileName = "powershell.exe"
$processStartInfo.Arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$scriptPath`""
$processStartInfo.Verb = "RunAs"

# 启动新的 PowerShell 进程
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $processStartInfo
$process.Start()

现在,我们可以创建一个`System.Diagnostics.Process`对象,并将进程的启动信息设置为之前创建的`processStartInfo`对象。最后,调用`Start()`方法启动新的 PowerShell 进程,并以管理员权限运行脚本。

请注意,在执行脚本时,系统将提示用户授权以管理员身份运行 PowerShell。用户需要提供管理员凭据才能成功执行脚本。

通过使用以上方法,您可以确保您的 PowerShell 脚本以管理员权限运行,以便执行需要管理员权限的操作。

四、程序相关界面:

delphi代码界面: 

运行结果:已经创建sharefolder共享目录。 

猜你喜欢

转载自blog.csdn.net/winniezhang/article/details/134592846