使用命名参数调用命令Start-Process

时间:2017-07-14 15:50:16

标签: powershell named-parameters invoke-command

我对PowerShell完全陌生。 我尝试做的就是使用命名参数在远程计算机上调用.exe。

$arguments = "-clientId TX7283 -batch Batch82Y7"
invoke-command -computername FRB-TER1 { Start-Process -FilePath "C:\Program Files (x86)\Acorne\LoadDen.exe" -ArgumemtList $arguments}

我收到此错误。

A parameter cannot be found that matches parameter name 'ArgumemtList'.
+ CategoryInfo: InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound, Microsoft.PowerShell.Commands.StartProcessCommand
+ PSComputerName : FRB-TER1

ArgumentList可能不喜欢参数名称。不确定。

3 个答案:

答案 0 :(得分:1)

这应该做你的工作:

$arguments = "-clientId TX7283 -batch Batch82Y7"
invoke-command -computername FRB-TER1 {param($arguments) Start-Process -FilePath "C:\Program Files (x86)\Acorne\LoadDen.exe" -ArgumentList $arguments} -ArgumentList $arguments

答案 1 :(得分:0)

试试这个:

 # Lets store each cmd parameter in an array
 $arguments = @()
 $arguments += "-clientId TX7283"
 $arguments += "-batch Batch82Y7"
 invoke-command -computername FRB-TER1 { 
     param (
        [string[]]
        $receivedArguments
     ) 

     # Start-Process now receives an array with arguments
     Start-Process -FilePath "C:\Program Files (x86)\Acorne\LoadDen.exe" -ArgumemtList $receivedArguments
  } -ArgumentList @(,$arguments) # Ensure that PS passes $arguments as array

答案 2 :(得分:0)

要将本地变量传递给远程执行的scriptblock,您还可以使用$Using:Varname(来自Posh Version 3.0)。请参阅Invoke-Command的帮助:

> help Invoke-Command -Full |Select-String -Pattern '\$using' -Context 1,7
     PS C:\> Invoke-Command -ComputerName Server01 -ScriptBlock {Get-EventLog
>    -LogName $Using:MWFO_Log -Newest 10}

     This example shows how to include the values of local variables in a
     command run on a remote computer. The command uses the Using scope
     modifier to identify a local variable in a remote command. By default, all
     variables are assumed to be defined in the remote session. The Using scope
     modifier was introduced in Windows PowerShell 3.0. For more information
     about the Using scope modifier, see about_Remote_Variables
  • 如果嵌套了脚本块,则可能需要重复$using:varname请参阅this reference

所以这也应该有效(未经测试)

$arguments = "-clientId TX7283 -batch Batch82Y7"
Invoke-Command -computername FRB-TER1 {Start-Process -FilePath "C:\Program Files (x86)\Acorne\LoadDen.exe" $Using:arguments}