2013-02-15 14 views
5

paramsを取り込んで関数を使用するpowershellスクリプトを作成したいと思います。powershellスクリプトのparams *と*関数

が、私はこの試みた:

param 
(
    $arg 
) 

Func $arg; 


function Func($arg) 
{ 
    Write-Output $arg; 
} 

を私はこれだ:

The term 'Func' 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. 
At func.ps1:6 char:5 
+ Func <<<< $arg; 
    + CategoryInfo   : ObjectNotFound: (Func:String) [], CommandNotFoundException 
    + FullyQualifiedErrorId : CommandNotFoundException 

ファインを、私は思いました。

function Func($arg) 
{ 
    Write-Output $arg; 
} 


param 
(
    $arg 
) 

Func $arg; 

しかし、その後、私はこれを得た:私の代わりにこれを試してみましょう

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. 
At C:\Users\akina\Documents\Work\ADDC\func.ps1:7 char:10 
+  param <<<< 
    + CategoryInfo   : ObjectNotFound: (param:String) [], CommandNotFoundException 
    + FullyQualifiedErrorId : CommandNotFoundException 

は私がなんとかするために求めているとは何ですか?あるいは、私の要求に不合理であるか?

+2

powershellスクリプトの順序は、通常、1)Params、2)関数3)関数呼び出し/実行するコマンドレット順です。 –

+0

Christopher Ranney、それは有用な集計です。これを質問として投稿した場合、私はそれを投票したでしょう。 –

+0

あなたの心が欲しいなら、あなたはコメントを投票することができます。 :) –

答えて

19

スクリプト内のparamブロックは、最初の非コメントコードである必要があります。デフォルトの動作では、出力ストリームに出力オブジェクトにあるため、その後、あなたは例えばそれを呼び出す前に、関数を定義する必要があります。:

param 
(
    $arg 
) 

function Func($arg) 
{ 
    $arg 
} 

Func $arg 

書き込み出力はあなたの例では不要です。

+0

そして、その関数定義をモジュールに移して私たちのマシンでそれをインポートしたら、Odejayiがしようとしている@Tolaのように呼び出すのはどうでしょうか? –

+0

@FarruhhWaheed同じことがモジュールに適用されます。おそらく典型的なケースではありませんが、PSM1は、通常のスクリプトファイルと同様にパラメータを定義し、コードを実行できます(関数を定義するだけでなく)。 –

-2

あなたは...関数内でこのような

何かparamタグを置くことができます:あなたが必要とするすべてのPARAMは、スクリプトの最初の文字列であることを確認するためにIUS

function func($avg) 
{ 
    param 
    (
     $avg 
    ) 
} 
3

を。

関連する問題