如何运行 PowerShell 脚本?
我有一个名为 myscript.ps1 的脚本
我已经安装了所有必要的框架
我设置了执行策略
我已按照此 MSDN 帮助页面上的说明进行操作,并尝试像这样运行它: powershell.exe 'C:\my_path\yada_yada\run_import_script.ps1' (带或不带 --noexit)
除了输出文件名外,它什么也不返回。
没有错误,没有消息,什么都没有。哦,当我添加 -noexit
时,会发生同样的事情,但我仍然在 PowerShell 中并且必须手动退出。
.ps1 文件应该运行一个程序并根据该程序的输出返回错误级别。但我很确定我还没有到达那里。
我究竟做错了什么?
cmd
一样启动 powershell
。现在您可以将 myscript.ps1
脚本作为其中的任何可执行文件执行(在 powershell 窗口中),即 .\myscript.ps1
先决条件:
您需要能够以管理员身份运行 PowerShell
您需要将 PowerShell 执行策略设置为允许值或能够绕过它
脚步:
以管理员身份启动 Windows PowerShell,并等待 PS> 提示出现 在 PowerShell 中导航到脚本所在的目录: PS> cd C:\my_path\yada_yada\(输入)执行脚本: PS> .\run_import_script。 ps1(输入)
或者:您可以从命令提示符 (cmd.exe
) 运行 PowerShell 脚本,如下所示:
powershell -noexit "& ""C:\my_path\yada_yada\run_import_script.ps1""" (enter)
根据 Kirk Munro 的Invoking a PowerShell script from cmd.exe (or Start | Run)。
或者您甚至可以run your PowerShell script asynchronously from your C# application。
如果您使用的是 PowerShell 2.0,请使用 PowerShell.exe 的 -File
参数从另一个环境(如 cmd.exe)调用脚本。例如:
Powershell.exe -File C:\my_path\yada_yada\run_import_script.ps1
-ExecutionPolicy Bypass
如果要在不修改默认脚本执行策略的情况下运行脚本,可以在启动 Windows PowerShell 时使用绕过开关。
powershell [-noexit] -executionpolicy bypass -File <Filename>
-nologo
选项以删除启动横幅
类型:
powershell -executionpolicy bypass -File .\Test.ps1
注意:这里的 Test.ps1
是 PowerShell 脚本。
powershell -executionpolicy bypass -File .\Test.ps1
执行,假设您当前的工作目录包含 Test.ps1
我遇到了同样的问题,我尝试了又尝试......最后我使用了:
powershell.exe -noexit "& 'c:\Data\ScheduledScripts\ShutdownVM.ps1'"
并将这一行放入批处理文件中,这样就可以了。
如果您只有 PowerShell 1.0,这似乎可以很好地解决问题:
powershell -command - < c:\mypath\myscript.ps1
它将脚本文件通过管道传送到 PowerShell 命令行。
相当容易。右键单击 Windows 中的 .ps1 文件,然后在 shell 菜单中单击使用 PowerShell 运行。
使用 cmd (BAT) 文件:
@echo off
color 1F
echo.
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "PrepareEnvironment.ps1"
:EOF
echo Waiting seconds
timeout /t 10 /nobreak > NUL
如果您需要以管理员身份运行:
创建一个指向命令提示符的快捷方式(我将其命名为管理命令提示符) 打开快捷方式的属性并转到“兼容性”选项卡 在“权限级别”部分下,确保选中“以管理员身份运行此程序”旁边的复选框
我有一个非常简单的答案:
以管理员模式打开 PowerShell 运行:set-executionpolicy unrestricted 打开常规 PowerShell 窗口并运行您的脚本。
我在作为错误消息的一部分给出的链接之后找到了这个解决方案:About Execution Policies
编辑:确保完成后运行 set-ExecutionPolicy default
,否则您将面临安全风险(感谢匿名用户)。
set-ExecutionPolicy default
,否则您将面临安全风险。
如果您的脚本以 .ps1
扩展名命名并且您在 PowerShell 窗口中,则只需运行 ./myscript.ps1
(假设文件在您的工作目录中)。
无论如何,在带有 PowerShell 版本 5.1 的 Windows 10 上这对我来说都是正确的,而且我认为我没有做任何事情来使它成为可能。
myscript.ps1
不起作用,引发错误,但使用 ./
它正在执行。
如果您想使用 Windows 任务计划程序运行 PowerShell 脚本,请按照以下步骤操作:
创建任务 将程序/脚本设置为 Powershell.exe 将参数设置为 -File "C:\xxx.ps1"
它来自另一个答案,How do I execute a PowerShell script automatically using Windows task scheduler?。
给出脚本的路径,即cmd设置的路径: $> 。 c:\程序文件\prog.ps1
运行 PowerShell 的入口点函数:例如 $> add 或 entry_func 或 main
>$ . c:\program file\prog.ps1 '.' is not recognized as an internal or external command, operable program or batch file.
和 >$ add or entry_func or main 'add' is not recognized as an internal or external command, operable program or batch file.
您可以像这样从 cmd 运行:
type "script_path" | powershell.exe -c -
在文件名前使用 -File
参数。引号使 PowerShell 认为它是一串命令。
使用适当的执行策略,您应该能够直接调用该文件,Windows 会将其与 PowerShell 关联
C:\my_path\yada_yada\run_import_script.ps1
这对争论没有那么好。您问题的真正答案是您缺少 &
来说“执行此”
powershell.exe '& C:\my_path\yada_yada\run_import_script.ps1'
打开 Power-shell 移动 .ps1 文件可用的文件夹,例如。 cd c:\users
这应该执行您的简单 powershell 脚本。
我刚刚找到了当我们右键单击 ps1
脚本并单击“使用 PowerShell 运行”时 Microsoft 所做的方法:
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & 'C:\Users\USERNAME\Desktop\MYSCRIPT.ps1'"
不定期副业成功案例分享
powershell.exe
然后脚本文件的方式被搞砸了。你知道如何修改它吗?powershell -noexit "& "C:\yada_yada\run_import_script.ps1"
(注意 three 双引号)我不太明白为什么,但在这一点上,我真的不在乎 :) 非常感谢!"&
究竟是做什么的?powershell c:\mypath\yadayada\myimportantscript.ps1
,但如果您在其周围加上引号,powershell 将尝试将该参数解释为 powershell 命令字符串。