将参数传递给PS文件时,如何使用invoke-command从Windows命令行运行PS文件?
这是我的脚本:
#TestParams2.ps1
param (
[string]$Server,
[string]$FileFolderName,
[string]$VerValue
)
$Server
$FileFolderName
$VerValue
这就是我试图称呼它的方式:
powershell.exe invoke-command -FilePath 'C:\temp\TestParams2.ps1' -ArgumentList 'myserver','\\server1\folder name\file.txt','9999'
当我运行它时,我得到了:
Invoke-Command : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
+ invoke-command -FilePath 'C:\temp\TestParams2.ps1' -ArgumentList 'mys ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeCommandCommand
我该如何称呼脚本?
最佳答案
zdan's answer包含关键指针: -FilePath
参数仅用于远程处理的上下文中,因此除非您还指定了-ComputerName
参数,否则不能使用它。
但是,仅使用-ComputerName
以便您可以定位本地计算机(a)效率低下(b),更重要的是,它需要以admin(提升)身份运行。
根本不需要Invoke-Command
;而是使用-File
CLI参数调用脚本,并像往常一样传递参数,并用空格分隔:
powershell.exe -File C:\temp\TestParams2.ps1 myserver "\\server1\folder name\file.txt" 9999
如果还需要绕过有效的execution policy:
警告:如果有效执行策略由组策略控制,则不能覆盖它。
powershell.exe -ExecutionPolicy Bypass -File C:\temp\TestParams2.ps1 myserver "\\server1\folder name\file.txt" 9999
请注意,必须将
-ExecutionPolicy Bypass
放在-File
参数之前(类似地使用-Command
参数)--File
之后的任何内容都被视为目标脚本路径以及传递给它的任何参数。
关于windows - 将参数传递给PS文件时,如何使用invoke-command从Windows命令行运行PS文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58719935/