运行 PowerShell 脚本作时出现“无法运行 PowerShell 脚本”错误

本文提供了在 Microsoft Power Automate for desktop 中运行 PowerShell 脚本 作时发生的错误的解决方案。

症状

在 Power Automate for Desktop 中执行桌面流期间, 运行“运行 PowerShell 脚本 ”作时会出现错误。 在最近的 Windows 更新之后,也可能出现此问题。

错误消息如下所示:

Microsoft.Flow.RPA.Desktop.Modules.SDK.ActionException: Failed to run PowerShell script. ---> System.ComponentModel.Win32Exception: The system cannot find the file specified
   at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
   at Microsoft.Flow.RPA.Desktop.Modules.System.Actions.SystemActions.RunPowershellScript(Variant powershellCode, Variant& scriptStandardOutput, Variant& scriptErrorOutput)
   --- End of inner exception stack trace ---
   at Microsoft.Flow.RPA.Desktop.Modules.System.Actions.RunPowershellScript.Execute(ActionContext context)
   at Microsoft.Flow.RPA.Desktop.Robin.Engine.Execution.ActionRunner.Run(IActionStatement statement, Dictionary`2 inputArguments, Dictionary`2 outputArguments)

原因

运行 PowerShell 脚本动作在内部启动一个powershell.exe实例,并将动作输入中指定的脚本作为进程的参数。 如果系统找不到 powershell.exe,可能会收到错误消息。

此问题的最可能原因是 Path 环境变量不包含包含可执行文件的 powershell.exe 目录。 若要确认这是根本原因,请执行以下步骤:

  1. 打开命令提示符(CMD)窗口。
  2. 键入命令并按 Enter 运行powershell.exe

如果出现以下消息,则问题在于 Path 环境变量中缺少的路径powershell.exe

“powershell.exe”无法识别为内部或外部命令、可作的程序或批处理文件。

决议

若要解决此问题,请按照以下步骤更新 Path 环境变量以包含可执行文件的 powershell.exe 目录:

  1. 打开“开始”菜单,搜索 环境变量,然后选择“ 编辑系统环境变量”。

  2. 在“ 系统属性” 窗口中,选择“ 环境变量”。

  3. “系统变量 ”部分下,找到并选择 路径 变量,然后选择“ 编辑”。

  4. 将目录路径 powershell.exe 添加到路径列表。

    在大多数情况下,缺少的路径为 C:\WINDOWS\System32\WindowsPowerShell\v1.0\

    若要确认正确的路径,请打开 PowerShell 终端并运行 $PsHome 命令。

    使用显示的路径作为要添加到 Path 变量的值。

  5. 选择 “确定 ”以保存更改并关闭所有对话框。