Invoke-Command Start-Process 带命名参数

Invoke-Command Start-Process with named parameters

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

这应该完成你的工作:

$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

试试这个:

 # 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

要将局部变量传递给远程执行的脚本块,您还可以使用 $Using:Varname(从 Posh 版本 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}