2015-10-19 43 views
1

我有以下(不工作)poweshell腳本:如何在Powershell中將參數傳遞給Invoke-Expression?

$scriptPath = ((new-object net.webclient).DownloadString('https://gist.githubus 
ercontent.com/AndrewSav/c4fb71ae1b379901ad90/raw/23f2d8d5fb8c9c50342ac431cc0360ce44465308/SO33205298')); $args = "`"aaa 
bbb`""; iex $scriptPath $args 

所以我:

  1. 下載腳本來執行。
  2. 設置我的參數列表發送到腳本
  3. 執行腳本(對我來說,這是在現在的cli)。

但它的示數:

Invoke-Expression : A positional parameter cannot be found that accepts argument '"aaa bbb"'. 
At line:1 char:209 
+ ... 44465308/SO33205298')); $args = "`"aaa bbb`""; iex $scriptPath $args 
+              ~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidArgument: (:) [Invoke-Expression], ParentContainsErrorRecordException 
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeExpressionCommand 

誰能幫我找出如何在參數傳遞給該腳本傳遞?

注意:此問題引用/產生於this other SO question

回答

1

你應該嘗試這樣的事:

$scriptPath = ((new-object net.webclient).DownloadString('https://gist.githubusercontent.com/AndrewSav/c4fb71ae1b379901ad90/raw/23f2d8d5fb8c9c50342ac431cc0360ce44465308/SO33205298')) 
Invoke-Command -ScriptBlock ([scriptblock]::Create($scriptPath)) -ArgumentList "coucou" 

你必須調用它之前創建的源腳本塊。

+0

>「在調用它之前,您必須從源創建一個ScriptBlock。」 - 事實上並非如此。您可以使用IEX,II和&call運算符來調用Posh代碼的字符串表示形式。 – ChadT

+0

@DaRKoN_and ..我可以傳入命名參數嗎?例如。 '-version 1.2.3.4 -source pewPew -foo whatever' ?? < - 並且這些參數與腳本中列出的參數的順序不同 –

+0

@DaRKoN_您可以調用腳本,但$ Scriptpath不是腳本路徑,而是腳本的源,而且我不請參閱如何使用Invoke-Item從源文件啓動腳本,而不是將源文件保存到文件。我在這裏給出的答案。 – JPBlanc

相關問題