PowerShellHelperExecuteFileAsync 方法

异步执行 PowerShell 脚本文件并获取输出。

Definition

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

参数

scriptPath  String
.ps1 脚本文件的完整路径或相对路径。
arguments  String  (Optional)
传给脚本的参数(原样拼接在 -File 之后,调用方需自行处理引号与转义)。
workingDirectory  String  (Optional)
工作目录;为 null 时使用脚本文件所在目录。
executionPolicy  String  (Optional)
执行策略,默认 "Bypass"。
cancellationToken  CancellationToken  (Optional)
取消令牌;取消时会终止整个进程树并重新抛出 OperationCanceledException。

返回值

TaskPowerShellHelperPsResult
包含退出码、标准输出和标准错误的 PowerShellHelperPsResult。

备注

stdout 与 stderr 通过两个独立任务并行读取,避免输出量大时因缓冲区写满而死锁。 取消时使用 Kill(entireProcessTree: true) 终止整个进程树,确保子进程一并退出。

异常

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

参见