2017-07-14 102 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}