2017-02-07 5 views
0

は、私は専門用語悪用てるなら、私を修正しますが、ここで私は、一般的な機能を持っているものです。set powershell関数を新しいより指定された関数にラップする方法は?

function runNode ([string][email protected]() , $fwd=".\", [string]$app="", [switch]$Process, [switch]$NoNewWindow) 
{ 
$appName=$app+"Node" 
$processNode=start-process node -ArgumentList:$argList -WorkingDirectory:$fwd -PassThru -NoNewWindow:$NoNewWindow 
} 

私はそのような何かを(、動作しない[function]タイプを見つけることができません)

[function]$myRunNode=runNode -argList "$projectFolder/node_modules/grunt/bin/grunt","serve","flag1" -fwd $projectFolder -app "appName" -Process 
たい

私はそのようなことをすることはできますか?私はパラメータを修正するだけです。私は関数呼び出しをラップすることができますが、パラメータを渡すのは非常に面倒です。デフォルトのパラメータを追加することはできません - そのようないくつかの指定された関数が必要です。

答えて

2

何がしたいことは返すことです:あなたは、匿名関数を定義するスクリプトブロックを使用する場合は

function myRunNode($ProjectFolder) { 
    runNode -argList "$ProjectFolder/node_modules/grunt/bin/grunt","serve","flag1" -fwd $ProjectFolder -app "appName" -Process 
} 

myRunNode -ProjectFolder 'C:\some\folder' 

を関数からのスクリプトブロック(実行可能なコードブロック)。また、あなたが後でそれらを使用したい場合は、関数内の変数に値を代入避けたいことがあります。

function Get-NodeRunner { 
    param(
     [string[]]$argList, 
     [string]$fwd = $pwd, 
     [string]$app, 
     [switch]$Process 
    ) 

    return { 
     param(
      [switch]$NoNewWindow 
     ) 

     $StartProcessParams = @{ 
      FilePath = 'node' 
      ArgumentList = $argList 
      WorkingDirectory = $fwd 
      PassThru = $Process.IsPresent 
      NoNewWindow = $NoNewWindow 
     } 

     return New-Object psobject -Property @{ 
      AppName = "${app}Node" 
      Process = Start-Process @StartProcessParams 
     } 
    }.GetNewClosure() 
} 

GetNewClosure()方法が近い渡されたパラメータを含むGet-NodeRunner関数内の変数、上のスクリプトブロックを持つことになりますし、次に行うことができます:

# Generate your function 
$NodeRunner = Get-NodeRunner -argList "$projectFolder/node_modules/grunt/bin/grunt","serve","flag1" -fwd $projectFolder -app "appName" -Process 

# Run it using the call operator (&) 
& $NodeRunner -NoNewWindow:$false 
1

あなたが機能をラップしたい場合は、単に機能ラップ:

$myRunNode = { 
    runNode -argList "$projectFolder/node_modules/grunt/bin/grunt","serve","flag1" -fwd $projectFolder -app "appName" -Process 
} 

$projectFolder = 'C:\some\folder' 
$myRunNode.Invoke() 
関連する問題