2017-07-14 110 views
1

我完全是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可能不喜欢参数名称。不确定。

回答

1

这应该做你的工作:

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

我需要-ArgumentList $参数两次吗?尽管如此,仍然给我同样的错误。 – zorrinn

+0

我删除了额外的ArgumentList并尝试。这次它说, 无法验证参数'ArgumentList'的参数。参数为空或空。 – zorrinn

+0

@zorrinn:那不是额外的。最后的参数列表用于传递invoke-command的scriptblock内的值。帕拉姆用于在块内接受它。最后它会开始处理你真正想要传递的arg列表 –

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 
0

一个局部变量传递给执行远程您还可以使用$Using:Varname(从辣妹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 
  • 如果scriptblocks嵌套您可能需要重复$using:varnamethis reference

所以这应该工作太(未经测试)

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