You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中C#执行PowerShell脚本遇“未数字签名”错误求助

解决Unity通过C#调用PowerShell脚本时的“未数字签名”错误

你在Unity中用C#调用PowerShell脚本的代码如下:

ProcessStartInfo startInfo = new ProcessStartInfo()
{
    FileName = "powershell.exe",
    Arguments = $"-ExecutionPolicy Unrestricted -file \"{_path}\" -filePath \"{_filePath}\"",
    UseShellExecute = false,
    RedirectStandardOutput = true,
    RedirectStandardError = true
};
Process test = Process.Start(startInfo);

运行时遇到“文件{路径}\command.ps1 is not digitally signed”错误,试过-ExecutionPolicy Unrestricted和-ExecutionPolicy Bypass都没用,可能是以下几个原因:

  • 加-NoProfile参数跳过配置文件干扰:PowerShell启动时会加载用户配置文件,里面的设置可能会覆盖你指定的执行策略。在参数开头加上-NoProfile,确保当前实例不加载任何配置:

    Arguments = $"-NoProfile -ExecutionPolicy Bypass -file \"{_path}\" -filePath \"{_filePath}\""
    
  • 注意PowerShell的架构匹配:Unity默认是32位进程,会调用32位的powershell.exe(路径在C:\Windows\SysWOW64\WindowsPowerShell\v1.0\),而你手动设置的执行策略可能只针对64位生效。可以:

    • 打开32位PowerShell,执行Set-ExecutionPolicy Bypass -Scope CurrentUser来设置32位环境的执行策略
    • 或者直接在代码里指定对应架构的PowerShell路径
  • 改用-Command参数调用脚本:-file参数对执行策略的检查更严格,换成-Command方式调用,执行策略限制会更宽松:

    Arguments = $"-NoProfile -ExecutionPolicy Bypass -Command & \"{_path}\" -filePath \"{_filePath}\""
    
  • 检查脚本路径的转义问题:确保_path变量里的路径没有未正确转义的特殊字符,比如C#字符串里的路径要用\\或者加@前缀避免转义错误。

内容的提问来源于stack exchange,提问作者PugWrath

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 02:35:18