【问题标题】:execution policy error while calling powershell script from batch file, powershell v 1.0从批处理文件调用powershell脚本时执行策略错误,powershell v 1.0
【发布时间】:2013-02-20 01:32:29
【问题描述】:

我创建了一个 PowerShell 脚本来从子文件夹中获取文件。这在我手动输入Set-ExecutionPolicy unrestricted 后从 PowerShell 控制台运行时执行,但是当我从批处理文件中调用相同的脚本时

C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe D:\programs\abc\bin\xyz_clean.ps1

它会抛出一个错误:

xyz_clean.ps1 无法加载,因为此系统上禁用了脚本执行。

【问题讨论】:

  • 您运行的是什么操作系统(包括 32 位和 64 位)?
  • 您是否尝试过在批处理中指定执行策略? C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -executionpolicy unrestricted -file D:\programs\abc\bin\xyz_clean.ps1 除此之外,您的“per user”和“machine”执行策略之间可能存在问题,您可以尝试使用 set-executionpolicy 的-scope 参数
  • 您缺少 -file 选项。组策略是否拒绝了 powershell 脚本?
  • 另外,您确定您正在运行 PowerShell v1 吗?可执行文件的路径并不能证明这一点,因为它在 PS2 和 PS3 中没有改变。尝试检查$psversiontable 变量的PSVersion 属性。
  • 谢谢回复我的批处理文件看起来像

标签: powershell batch-file


【解决方案1】:

您可以从命令窗口调用 powershell:

C:\> powershell /?
C:\> powershell -executionpolicy unrestricted -file \\server\file.ps1

然后将其保存到 .bat 文件中:

C:\> echo powershell -executionpolicy unrestricted -file \\server\file.ps1 > file.bat

【讨论】:

    猜你喜欢
    • 2023-03-30
    • 2014-10-14
    • 2023-03-30
    • 2019-11-04
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    相关资源
    最近更新 更多