如何运行PowerShell脚本?

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

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

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

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

我做错了什么?


当前回答

类型:

powershell -executionpolicy bypass -File .\Test.ps1

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

其他回答

我也遇到过同样的问题,我试了又试……最后我用了:

powershell.exe -noexit "& 'c:\Data\ScheduledScripts\ShutdownVM.ps1'"

把这一行放到批处理文件中,就可以了。

类型:

powershell -executionpolicy bypass -File .\Test.ps1

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

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

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

如果您使用的是PowerShell 2.0,请使用PowerShell.exe的-File参数从另一个环境(如cmd.exe)调用脚本。例如:

Powershell.exe -File C:\my_path\yada_yada\run_import_script.ps1

一个简单的方法是使用PowerShell ISE,打开脚本,运行和调用你的脚本,函数…