PowerShellHelperExecuteFileWithProgressAsync 方法

异步执行 PowerShell 脚本文件,并实时报告每一行输出。

Definition

命名空间: Suresking.Utility
程序集: Suresking.Utility (在 Suresking.Utility.dll 中) 版本:1.3.22+a27276bb8ffde055e69ff608e98aafba279e1e99
C#
public static Task<int> ExecuteFileWithProgressAsync(
	string scriptPath,
	string? arguments = null,
	string? workingDirectory = null,
	IProgress<string>? progress = null,
	string executionPolicy = "Bypass",
	CancellationToken cancellationToken = default
)

参数

scriptPath  String
.ps1 脚本文件的完整路径或相对路径。
arguments  String  (Optional)
传给脚本的参数(原样拼接在 -File 之后)。
workingDirectory  String  (Optional)
工作目录;为 null 时使用脚本文件所在目录。
progress  IProgressString  (Optional)
实时进度回调,每读到一行输出即报告一次;来自 stderr 的行会带 "[ERR] " 前缀。 为 null 时不报告进度(输出被丢弃)。
executionPolicy  String  (Optional)
执行策略,默认 "Bypass"。
cancellationToken  CancellationToken  (Optional)
取消令牌;取消时会终止整个进程树并重新抛出 OperationCanceledException。

返回值

TaskInt32
进程退出码。

备注

此方法不返回完整输出字符串,仅通过 progress 逐行推送, 适合输出量大、需要实时展示进度的场景;如需完整输出请使用 ExecuteFileAsync(String, String, String, String, CancellationToken)。

stdout 与 stderr 由两个后台任务并行逐行读取,避免缓冲区阻塞。 取消时读取任务会因流关闭而自然结束。

异常

FileNotFoundException脚本文件不存在。
OperationCanceledException操作被 cancellationToken 取消。

参见