Powershell 脚本未从批处理文件触发

发布于 2024-10-14 19:25:22 字数 433 浏览 1 评论 0原文

我一直在尝试从批处理文件启动一个简单的 powershell 脚本。网上查了一下,建议是使用 Set-ExecutionPolicy 设置策略。

我已经完成了此操作并使用 Get-ExecutionPolicy 策略已更改。

但是,运行此批处理文件会导致出现“打开方式”对话框,而不是执行脚本。

Batch:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe ^&'./script.psl'

Powershell:

Write-Host "This is a message"

我在 Windows 7 和 Server 2008 R2 上进行了采样。两者都有相同的结果。我缺少什么?

I've been trying to launch a simple powershell script from a batch file. After looking online the advice is to set the policy using Set-ExecutionPolicy.

I've done this and using Get-ExecutionPolicy the policy has been changed.

However running this batch file results in an 'Open With' dialog rather than the execution of the script.

Batch:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe ^&'./script.psl'

Powershell:

Write-Host "This is a message"

I have sampled on both Windows 7 and Server 2008 R2. Both have same result. What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妄司 2024-10-21 19:25:22

要从命令行运行脚本文件,请使用 powershell.exe 的 -file 参数。

 %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe  -file './script.psl'

To run a script file from the command line, use the -file parameter of powershell.exe.

 %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe  -file './script.psl'
往事随风而去 2024-10-21 19:25:22

要从 *.cmd 文件运行脚本文件,请使用 powershell.exe 的 -file 参数和双引号:

 %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe  -file "./script.ps1"

当您在批处理文件中仅使用一个引号时,您可能会遇到如下 powershell 错误:

处理 -File ''./build.ps1'' 失败,因为该文件没有
“.ps1”扩展名。指定有效的 Windows PowerShell 脚本文件
名称,然后重试。

To run a script file from the *.cmd file , use the -file parameter of powershell.exe and double quotes:

 %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe  -file "./script.ps1"

When you will use only one quote in batch file you can expect powershell error like:

Processing -File ''./build.ps1'' failed because the file does not have
a '.ps1' extension. Specify a valid Windows PowerShell script file
name, and then try again.

柳絮泡泡 2024-10-21 19:25:22

还有另一种方法可以得到此错误,该错误与文件路径中的空格相关,即使到今天,这种情况在 Windows 中仍然很常见。

假设您已经了解了在 powershell 中的常识,您需要使用反引号 `(也称为坟墓运算符)转义文件名中的空格。

然后,您可以在批处理脚本中像这样使用它来运行 powershell 脚本,这两个脚本都位于其中包含空格的目录路径的同一子目录中:

@REM get directory of the batch file script to use for powershell script
set cwd=%~dp0
@REM escape the spaces for powershell
set cwd=%cwd: =` %
echo %cwd%
@REM list the powershell script with a powershell command
powershell.exe -c Get-ChildItem "%cwd%install.ps1"
@REM run the powershell script
powershell.exe -File "%cwd%install.ps1"

Get-ChildItem 将起作用,但 powershell 将找不到该脚本作为 -File 的参数。
事实上,事实证明,-File 参数不需要转义空格,即使 Get-ChildItem 需要转义空格。所以这会起作用:

set cwd=%~dp0
set cwd1=%cwd: =` %
echo %cwd%
echo %cwd1%
powershell.exe -c Get-ChildItem "%cwd1%install.ps1"
powershell.exe -File "%cwd%install.ps1"

是的,我发现了这一点,因为我正在批处理脚本中运行 powershell 脚本,原因与OP相同。 13年后。

There is another way you can get this error, associated with spaces in file paths which is still common in windows even to this very day.

Let's say you had access to the common knowledge that in powershell, you need to escape spaces in filenames with a backtick, `, also known as a grave operator.

You could then use it like this in a batch script to run a powershell script, both of which are in the same sub-directory of a directory path that has spaces in it:

@REM get directory of the batch file script to use for powershell script
set cwd=%~dp0
@REM escape the spaces for powershell
set cwd=%cwd: =` %
echo %cwd%
@REM list the powershell script with a powershell command
powershell.exe -c Get-ChildItem "%cwd%install.ps1"
@REM run the powershell script
powershell.exe -File "%cwd%install.ps1"

Get-ChildItem will work, but powershell will not find the script as an argument of -File.
In fact, as it turns out, escaping the spaces is NOT needed for the -File argument, even as it is needed for Get-ChildItem. So this will work:

set cwd=%~dp0
set cwd1=%cwd: =` %
echo %cwd%
echo %cwd1%
powershell.exe -c Get-ChildItem "%cwd1%install.ps1"
powershell.exe -File "%cwd%install.ps1"

And yes I found this out because I'm running powershell script in batch script for same reason as OP. 13 years later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文