ChatGPT解决这个技术问题 Extra ChatGPT

如何运行 PowerShell 脚本

如何运行 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

T
TylerH

先决条件:

您需要能够以管理员身份运行 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.exe 然后脚本文件的方式被搞砸了。你知道如何修改它吗?
你的博客文章链接做到了。我必须使用 powershell -noexit "& "C:\yada_yada\run_import_script.ps1" (注意 three 双引号)我不太明白为什么,但在这一点上,我真的不在乎 :) 非常感谢!
"& 究竟是做什么的?
根据 technet.microsoft.com/en-us/library/ee176949.aspx,'&'是针对“如果您确实要执行该字符串值(即,如果您要运行其路径用双引号括起来的脚本),则需要在路径前加上调用运算符(与号)。”
如果您的路径和文件名中没有空格,则只需使用命令 powershell c:\mypath\yadayada\myimportantscript.ps1,但如果您在其周围加上引号,powershell 将尝试将该参数解释为 powershell 命令字符串。
P
Peter Mortensen

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

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

有没有办法为这样的调用添加参数?
您应该能够在脚本文件路径之后跟踪 args。从 PowerShell.exe 使用 - [-File ]
“无法加载,因为在此系统上禁用了脚本的执行。有关更多详细信息,请参阅“get-help about_signing”。”
如果您尚未在系统上启用 PowerShell 脚本执行,请添加参数 -ExecutionPolicy Bypass
仅供参考,根据我的经验,这也适用于 PowerShell 1.0(Windows 2012 Server)
C
Chingiz Musayev

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

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

此外,包括 -nologo 选项以删除启动横幅
i
igr

类型:

powershell -executionpolicy bypass -File .\Test.ps1

注意:这里的 Test.ps1 是 PowerShell 脚本。


这应该在 powershell 中作为 powershell -executionpolicy bypass -File .\Test.ps1 执行,假设您当前的工作目录包含 Test.ps1
P
Peter Mortensen

我遇到了同样的问题,我尝试了又尝试......最后我使用了:

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

并将这一行放入批处理文件中,这样就可以了。


P
Peter Mortensen

如果您只有 PowerShell 1.0,这似乎可以很好地解决问题:

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

它将脚本文件通过管道传送到 PowerShell 命令行。


当安全策略不允许脚本执行时有用的技巧。
P
Peter Mortensen

相当容易。右键单击 Windows 中的 .ps1 文件,然后在 shell 菜单中单击使用 PowerShell 运行。


这可以快速运行脚本,而无需使用执行策略启用脚本的执行。谢谢!
K
Kiquenet

使用 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

如果您需要以管理员身份运行:

创建一个指向命令提示符的快捷方式(我将其命名为管理命令提示符) 打开快捷方式的属性并转到“兼容性”选项卡 在“权限级别”部分下,确保选中“以管理员身份运行此程序”旁边的复选框


P
Peter Mortensen

一种简单的方法是使用 PowerShell ISE,打开脚本,运行并调用您的脚本、函数...

https://i.stack.imgur.com/h2jFM.png


J
JovanToroman

我有一个非常简单的答案:

以管理员模式打开 PowerShell 运行:set-executionpolicy unrestricted 打开常规 PowerShell 窗口并运行您的脚本。

我在作为错误消息的一部分给出的链接之后找到了这个解决方案:About Execution Policies

编辑:确保完成后运行 set-ExecutionPolicy default,否则您将面临安全风险(感谢匿名用户)。


确保在完成后运行 set-ExecutionPolicy default,否则您将面临安全风险。
P
Peter Mortensen

如果您的脚本以 .ps1 扩展名命名并且您在 PowerShell 窗口中,则只需运行 ./myscript.ps1(假设文件在您的工作目录中)。

无论如何,在带有 PowerShell 版本 5.1 的 Windows 10 上这对我来说都是正确的,而且我认为我没有做任何事情来使它成为可能。


这如何回答这个问题?
它绝对回答了这个问题:如何运行 powershell 脚本?答:启动powershell控制台,然后执行脚本。简单的。简单的。也适用于 Linux。
这绝对回答了这个问题,并且正是我想要的。由于 myscript.ps1 不起作用,引发错误,但使用 ./ 它正在执行。
P
Peter Mortensen

如果您想使用 Windows 任务计划程序运行 PowerShell 脚本,请按照以下步骤操作:

创建任务 将程序/脚本设置为 Powershell.exe 将参数设置为 -File "C:\xxx.ps1"

它来自另一个答案,How do I execute a PowerShell script automatically using Windows task scheduler?


P
Peter Mortensen

给出脚本的路径,即cmd设置的路径: $> 。 c:\程序文件\prog.ps1

运行 PowerShell 的入口点函数:例如 $> add 或 entry_func 或 main


从 cmd 提示符运行命令:>$ . 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.
W
Wasif

您可以像这样从 cmd 运行:

type "script_path" | powershell.exe -c -

P
Peter Mortensen

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


这么多年过去了,我仍然看不到运行命令的答案,就像从服务中的第三个重试选项一样,在不加载 PS $profile: 'powershell - command 'Start 的情况下使用这样的脚本启动服务-Service -Name '服务名称' -ErrorAction Continue -Verbose'
c
carrvo

使用适当的执行策略,您应该能够直接调用该文件,Windows 会将其与 PowerShell 关联

C:\my_path\yada_yada\run_import_script.ps1

这对争论没有那么好。您问题的真正答案是您缺少 & 来说“执行此”

powershell.exe '& C:\my_path\yada_yada\run_import_script.ps1'

r
rajeshmuthusamy

打开 Power-shell 移动 .ps1 文件可用的文件夹,例如。 cd c:\users\ 现在输入你的 powershell 文件名 ex.testfile.ps1 按 Enter

这应该执行您的简单 powershell 脚本。


这只是重复所有现有的答案。
R
Roukmoute

我刚刚找到了当我们右键单击 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'"