如何运行PowerShell脚本?

我有一个名为myscript.ps1的脚本 我已经安装了所有必要的框架 我设置了执行策略 我已经按照MSDN帮助页上的说明进行了操作 我试着这样运行它: powershell.exe“C: \ my_path \ yada_yada \ run_import_script。Ps1 '(有或没有——没有出口)

除了文件名是output之外,它什么也不返回。

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

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

我做错了什么?


当前回答

你可以像这样运行cmd:

type "script_path" | powershell.exe -c -

其他回答

类型:

powershell -executionpolicy bypass -File .\Test.ps1

注意:这里是测试。ps1为PowerShell脚本。

你可以像这样运行cmd:

type "script_path" | powershell.exe -c -

如果您希望在不修改默认脚本执行策略的情况下运行脚本,可以在启动Windows PowerShell时使用旁路开关。

powershell [-noexit] -executionpolicy bypass -File <Filename>

在文件名前使用-File参数。引号使PowerShell认为它是一串命令。

如果你只有PowerShell 1.0,这似乎做得很好:

powershell -command - < c:\mypath\myscript.ps1

它将脚本文件输送到PowerShell命令行。