windows - 将参数传递给PS文件时,如何使用invoke-command从Windows命令行运行PS文件?

标签 windows powershell batch-file

将参数传递给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/

相关文章:

c - 为什么DDK能够成功编译无效的源文件?

python - 如何正确安装 dulwich 以使 hg-git 在 Windows 上运行?

powershell - 在任务计划程序中传递Powershell参数

c# - 我可以使用 C# 和 Powershell 做什么?

windows - 使用windows批处理文件将多行带有特殊字符的行放入剪辑中

windows - 将所有批处理文件参数转发给内部命令

无法识别 Windows Embedded XP tskill 和 taskkill

regex - 方括号内字符的正则表达式

windows - 在批处理文件中运行命令行

scripting - 以批处理脚本将文件夹从一个目录移动到另一个目录