gpt4 book ai didi

windows - 如何运行 PowerShell 脚本

转载 作者:可可西里 更新时间:2023-11-01 11:50:42 24 4
gpt4 key购买 nike

如何运行 PowerShell 脚本?

  • 我有一个名为 myscript.ps1 的脚本
  • 我已经安装了所有必要的框架
  • 我设置了execution policy事情
  • 我已按照 this MSDN help page 上的说明进行操作并尝试像这样运行它:powershell.exe 'C:\my_path\yada_yada\run_import_script.ps1'(带或不带 --noexit)

除了输出文件名外,它什么都不返回。

没有错误,没有消息,什么都没有。哦,当我添加 -noexit 时,同样的事情发生了,但我仍然在 PowerShell 中并且必须手动退出。

.ps1 文件应该运行一个程序并根据该程序的输出返回错误级别。但我很确定我什至还没有到达那里。

我做错了什么?

最佳答案

先决条件:

  • 您需要能够以管理员身份运行 PowerShell
  • 您需要将 PowerShell 执行策略设置为允许值或能够绕过它

步骤:

  1. 以管理员身份启动 Windows PowerShell,并等待 PS> 提示出现

  2. 在 PowerShell 中导航到脚本所在的目录:

    PS> cd C:\my_path\yada_yada\ (enter)
  3. 执行脚本:

    PS> .\run_import_script.ps1 (enter)

或者:您可以从命令提示符 (cmd.exe) 运行 PowerShell 脚本,如下所示:

powershell -noexit "& ""C:\my_path\yada_yada\run_import_script.ps1""" (enter)

根据 Invoking a PowerShell script from cmd.exe (or Start | Run)柯克·蒙罗 (Kirk Munro) 着。

或者你甚至可以run your PowerShell script asynchronously from your C# application .

关于windows - 如何运行 PowerShell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2035193/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com