Power Shell HelperExecute File 方法
同步执行 PowerShell 脚本文件并获取输出。
Definition
命名空间: Suresking.Utility
程序集: Suresking.Utility (在 Suresking.Utility.dll 中) 版本:1.3.22+a27276bb8ffde055e69ff608e98aafba279e1e99
包含退出码、标准输出和标准错误的 PowerShellHelperPsResult。
程序集: Suresking.Utility (在 Suresking.Utility.dll 中) 版本:1.3.22+a27276bb8ffde055e69ff608e98aafba279e1e99
C#
public static PowerShellHelperPsResult ExecuteFile(
string scriptPath,
string? arguments = null,
string? workingDirectory = null,
string executionPolicy = "Bypass"
)参数
- scriptPath String
- .ps1 脚本文件的完整路径或相对路径。
- arguments String (Optional)
- 传给脚本的参数(原样拼接在 -File 之后,调用方需自行处理引号与转义)。
- workingDirectory String (Optional)
- 工作目录;为 null 时使用脚本文件所在目录。
- executionPolicy String (Optional)
- 执行策略,默认 "Bypass"(不阻止任何脚本运行)。
返回值
PowerShellHelperPsResult包含退出码、标准输出和标准错误的 PowerShellHelperPsResult。
备注
此方法会阻塞当前线程直到进程结束,并一次性读取全部输出, 不适合输出量极大或需要实时进度的场景;后者请使用 ExecuteFileWithProgressAsync(String, String, String, IProgressString, String, CancellationToken)。
若目标进程输出量很大,同步读取 stdout/stderr 存在缓冲区写满导致阻塞的风险(本方法为顺序读取), 极端场景建议使用异步版本。
异常
| FileNotFoundException | 脚本文件不存在。 |