如何从bat文件中运行powershell脚本文件并传递多个参数

如何从bat文件中运行powershell脚本文件并传递多个参数,powershell,batch-file,Powershell,Batch File,我想调用test.bat,它会再次使用文件test触发Powershell.exe 测试。蝙蝠: Powershell.exe -ExecutionPolicy Bypass -File "test1.ps1 -Param1 Value1 -Param2 Value2" param{$Tag,$CommitId} Write-Host $Tag Write-Host $CommitId test1.ps1: Powershell.exe -ExecutionPolicy Bypass -F

我想调用test.bat,它会再次使用文件test触发Powershell.exe

测试。蝙蝠:

Powershell.exe -ExecutionPolicy Bypass -File "test1.ps1 -Param1 Value1 -Param2 Value2"
param{$Tag,$CommitId}

Write-Host $Tag
Write-Host $CommitId
test1.ps1:

Powershell.exe -ExecutionPolicy Bypass -File "test1.ps1 -Param1 Value1 -Param2 Value2"
param{$Tag,$CommitId}

Write-Host $Tag
Write-Host $CommitId
两个文件都放在同一个目录中

目前我得到一个错误,我的文件没有.ps1扩展名,但这不是真的。。。但我想那是因为我以错误的方式传递参数


那么如何正确地将参数传递给My.bat中的调用呢?

好的,那么从batch test.bat来看可能是这样的

@echo off
set Param1="some text"
set Param2="Some more text"

test1.ps1 %Param1% %Param2%
这就是我将参数传递给powershell脚本的方式。您不必使用所有其他参数运行powershell.exe

在PS1文件中,我将执行以下操作:

[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$Tag,

[Parameter(Mandatory=$True,Position=2)]
[string]$commitId
)

write-host $tag
write-host $CommitId
如果您绝对需要其他针对powershell.exe的开关,您可能需要

Powershell.exe -ExecutionPolicy Bypass -File "test1.ps1" %Param1% %Param2%

我会把test.bat改成

Powershell.exe -ExecutionPolicy Bypass -Command "test1.ps1 -Tag Value1 -CommitId Value2"
当然,将Value1和Value2更改为您想要通过的实际值。

在test.bat中

powershell -command "&{C:\temp\script.ps1 -Tag Value1 -CommitId Value2"}"

查看[这一点][1]。。。[1] :.[cmdletbinding]可能会帮助您解决问题,但不帮助“yourscript.ps1%IN%%OUT%d”我需要查看完整的语句。我是一名powershell初学者,已经后悔了…变量%Param1%的名称是否绑定到.ps1文件中的名称,还是顺序决定了%Param1%是否传递给最初定义的$Tag?如果我理解正确,则顺序由cmdlet绑定中的“位置”部分决定。因此,在上述代码段中,$标记将被分配%param1%的值,因为%param1%出现在cmdline的位置1。如果使用test1.ps1%param2%%param1%调用脚本,则会为$tag分配%param2%的值,这就是您的意思吗?是的,这就是我的意思,它可以按预期工作。这是一种自然的秩序!谢谢一帮推土工!你有证据证明这有效吗?它不能为我运行任何东西。