5

パラメータを取り込んで関数を使用するPowerShellスクリプトを作成したいと思います。

私はこれを試しました:

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

私が求めているのは実行可能ですか?それとも私は私の要求に無理がありますか?

4

3 に答える 3

19

スクリプト内の param ブロックは、コメント以外の最初のコードでなければなりません。その後、呼び出す前に関数を定義する必要があります。

param
(
  $arg
)

function Func($arg)
{
  $arg
}

Func $arg

デフォルトの動作はオブジェクトを出力ストリームに出力することであるため、この例では Write-Output は不要です。

于 2013-02-15T20:20:10.420 に答える
4

PARAM がスクリプトの最初の文字列であることを確認するために必要なのはそれだけです。

于 2015-01-11T17:57:08.850 に答える
-3

関数内に param タグを入れることができます。

このようなもの:

function func($avg)
{
    param
    (
         $avg
    )
}
于 2013-12-19T14:06:11.287 に答える