简体   繁体   English

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

[英]How do I use invoke-command to run a PS file from the windows command line while passing parameters to the PS file?

How do I use invoke-command to run a PS file from the windows command line while passing parameters to the PS file?在将参数传递给 PS 文件时,如何使用 invoke-command 从 windows 命令行运行 PS 文件? This is my script:这是我的脚本:

#TestParams2.ps1

param (
    [string]$Server,
    [string]$FileFolderName,
    [string]$VerValue
)

$Server
$FileFolderName
$VerValue

And this is how I'm trying to call it:这就是我试图称呼它的方式:

powershell.exe invoke-command -FilePath 'C:\temp\TestParams2.ps1' -ArgumentList 'myserver','\\server1\folder name\file.txt','9999'

And when I run it, I get this:当我运行它时,我得到了这个:

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

How should I call the script?我应该如何调用脚本?

zdan's answer contains the crucial pointer: the -FilePath parameter is only meant to be used in the context of remoting , so you cannot use it unless you also specify the -ComputerName parameter. zdan 的答案包含关键指针: -FilePath参数仅用于远程处理的上下文,因此除非您还指定-ComputerName参数,否则您不能使用它。

However, using -ComputerName just so you can target the local machine is (a) inefficient and (b), more importantly, requires running as admin (elevated).但是,仅使用-ComputerName来定位本地计算机(a)效率低下,(b)更重要的是,需要以管理员身份运行(提升)。

You don't need Invoke-Command at all;您根本不需要Invoke-Command instead, call your script with the -File CLI parameter , passing the arguments as usual, separated with spaces:相反,使用 -File CLI 参数调用您的脚本-File像往常一样传递 arguments,用空格分隔:

powershell.exe -File C:\temp\TestParams2.ps1 myserver "\\server1\folder name\file.txt" 9999

If you also need to bypass the effective execution policy :如果还需要绕过有效的执行策略

Caveat : If the effective execution policy is controlled by a Group Policy , it cannot be overridden.警告:如果有效的执行策略由组策略控制,则不能被覆盖。

powershell.exe -ExecutionPolicy Bypass -File C:\temp\TestParams2.ps1 myserver "\\server1\folder name\file.txt" 9999

Note that -ExecutionPolicy Bypass must be placed before the -File parameter (applies analogously to use of the -Command parameter) - anything after -File is considered the target script path plus any arguments to pass through to it.请注意, -ExecutionPolicy Bypass必须放在-File参数之前(类似于使用-Command参数) - 在 -File 之后的任何内容都被视为目标脚本路径以及要传递给它的任何-File

The error is complaining that the set of parameters that you supplied to invoke-command is not complete such that it could not resolve which parameter set to invoke-command to apply.该错误抱怨您提供给invoke-command的参数集不完整,因此它无法解析要应用哪个参数集来invoke-command

In this case, when you specify the -FilePath argument, you must supply -ComputerName as well:在这种情况下,当您指定-FilePath参数时,您还必须提供-ComputerName

powershell.exe invoke-command -FilePath 'C:\temp\TestParams2.ps1' -ArgumentList 'myserver','\\server1\folder name\file.txt','9999' -ComputerName localhost

You have 2 options here.您在这里有 2 个选项。

Option 1: Specify the arguments on your call选项 1:在您的通话中指定 arguments

powershell.exe invoke-command -FilePath 'C:\temp\TestParams2.ps1' -Server 'myserver' -FileFolderName'\\server1\folder name\file.txt' -VerValue '9999' 

Option 2: Enable positional parameters选项 2:启用位置参数

Change your Script params to:将您的脚本参数更改为:

param (
    [Parameter(Position = 0)]
    [string]$Server,
    [Parameter(Position = 1)]
    [string]$FileFolderName,
    [Parameter(Position = 2)]
    [string]$VerValue
)

Then call your script like this:然后像这样调用你的脚本:

powershell.exe invoke-command -FilePath 'C:\temp\TestParams2.ps1' 'myserver' '\\server1\folder name\file.txt' '9999' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM