2009-08-22 25 views
28

我運行Windows 7 RTM。 Powershell 2.0默認安裝。我使用優秀的Windows Powershell ISE來編輯我的腳本。我有以下腳本:PowerShell 2.0中帕拉姆關鍵字錯誤

Param($p) 
Param($d) 
echo $p $d 

我將腳本另存爲SayItAgain.ps1。當我嘗試從交互式shell像這樣運行此腳本:

./SayItAgain -p "Hello" 

我收到以下錯誤:

The term 'Param' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling o 
f the name, or if a path was included, verify that the path is correct and try again. 
At C:\users\cius\Code\powershell\SayItAgain.ps1:2 char:6 
+ Param <<<< ($destination) 
    + CategoryInfo   : ObjectNotFound: (Param:String) [], CommandNotFoundException 
    + FullyQualifiedErrorId : CommandNotFoundException 

這是一個已知的問題還是我只是用錯了?

+0

我想知道,如果實際錯誤在腳本的不同行上。你說你在呼喚./SayItAgain.ps1但錯誤說你叫FindAndCopyFiles.ps1。有沒有更多的代碼可以分享?有時候錯誤可能會指向錯誤的方向。錯誤可能在其他地方。 – 2009-08-22 05:22:55

+0

啊,我對此不一致表示歉意。我創建了SayItAgain作爲一個簡單的例子來追蹤問題。我複製了原始腳本中的錯誤錯誤。我將在我的附錄中解釋這一點。 – 2009-08-22 15:42:16

回答

26

我已經解決了這個問題。我對我的不完整和不一致的信息表示歉意。我已更正了問題的描述以使其準確無誤。儘管如此,我感謝你的幫助。

的問題的根源在於,我使用不當帕拉姆關鍵字多次。正確的用法是在一個帕拉姆聲明類似下面的內聲明多個參數:

Param($p, $d) 

這種用法在Windows PowerShell幫助文章「about_Functions」解釋。

49

是您的參數($ P)在你的腳本的第一行?如果不是,那可能會導致Param錯誤。確保你的參數($ p)是第一行。

+1

這是最有可能的問題,但第一個非註釋行就足夠了。 – 2009-08-22 06:23:11

+0

Jon Ingle接受的答案可以解決他的問題。我覺得這很可能是關於其他程序員將面臨的「params」問題以及提供的確切解決方案的問題。謝謝@David。 – 2014-10-23 07:17:13

+0

如果只有錯誤信息可以提到這個! – rob 2015-03-19 11:07:40

1

運行此腳本,

cls 

param([string]$Url, [string]$Template="CMSPUBLISHING#0") 

Write-Host "Url: $Url" 

當我註釋掉cls在它的工作上面,我得到了同樣的錯誤

The term 'param' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

#cls 

param([string]$Url, [string]$Template="CMSPUBLISHING#0") 

Write-Host "Url: $Url" 
+5

在使用param關鍵字之前,除了空格或註釋之外,不能有其他任何內容,這就是爲什麼您會收到此錯誤。 – 2011-10-03 15:59:44