2017-09-28 13 views
0

Powershellラッパースクリプトを使用して、最近lsコマンドを壊したアプリケーションを修正しようとしています。私のラッパーは呼び出すコマンドと同じ名前でなければならないので、私はinvoke-commandを使って元のコマンドを呼び出しています。Powershellのコマンドと同じ名前のラッパー関数?

# yarn broke 'ls' 
function yarn() { 
    $modifiedArgs = @() 
    foreach ($arg in $args) { 
     if ($arg -cmatch '^ls$') { 
      $arg = 'list' 
     } 
     $modifiedArgs = $modifiedArgs + $arg 
    } 
    invoke-command yarn -ArgumentList @modifiedArgs 
} 

しかしinvoke-commandはどのように変更されたパラメータを元のコマンドを実行することができます

Invoke-Command : Parameter set cannot be resolved using the specified named parameters 

で失敗しますか?

編集:私も試しました-ArgumentList (,$modifiedArgs) Passing array to another script with Invoke-Commandと私はまだ同じエラーが発生します。

編集:invoke-commandはリモーティングのみのように見えます。私もInvoke-Expression "& yarn $modifiedArgs"を試しましたが、それはその機能自体を実行します。 powershell docs&オペレータの作品パー

+0

"プロキシ機能"を書きたいとします。これに関するいくつかの良い情報が[MicrosoftのScripting Guyコラム](https://blogs.technet.microsoft.com/heyscriptingguy/2011/03/01/proxy-functions-spice-up-your-powershell-core-cmdlets)にあります。 /)、[Microsoft TechNetのスクリプトセンターリポジトリ](https://gallery.technet.microsoft.com/scriptcenter/New-ProxyCommand-a-simple-9735745e)、[目的のPowerShell(Windows ITPro)](http: //windowsitpro.com/blog/powershell-proxy-functions#)。さらに詳しい情報が必要な場合は、[このGoogle検索](https://www.google.com/search?q=Proxy+command+powershell)が便利です。 –

+0

ありがとう@JeffZeitlinしかし、これはバイナリコマンドです:私は他のpowershellをラップしていません。たとえそれがあったとしても、Scripting Guyの列ごとに元の関数のコードを抽出することは、ここではクルミを割るためにスレッジハンマーを使用するように思えます。 – mikemaccana

答えて

1

私はあなたがあなたの周りはプライベートにスコープラッパー関数を場合は、コマンドのフルパスを指定する必要が得ることができます信じて:

# yarn broke 'ls' 
function Private:yarn() { 
    $modifiedArgs = @() 
    foreach ($arg in $args) { 
     if ($arg -cmatch '^ls$') { 
      $arg = 'list' 
     } 
     $modifiedArgs += $arg 
    } 
    & yarn $modifiedArgs 
} 

これは、子スコープに見えるものから機能を防ぐことができますし、それは意志アプリケーションを使用することに戻ります。

+0

ありがとう、これは完全に動作します。 – mikemaccana

1

# yarn broke 'ls' 
function yarn() { 
    $modifiedArgs = @() 
    foreach ($arg in $args) { 
     if ($arg -cmatch '^ls$') { 
      $arg = 'list' 
     } 
     $modifiedArgs += $arg 
    } 
    & 'C:\Program Files (x86)\Yarn\bin\yarn.cmd' $modifiedArgs 
} 

明示的にコマンドパスを指定して関与していない他の答えを受け入れることはまだ幸せ

+1

'&(gcm yarn-type application-totalcount 1)' – PetSerAl

関連する問題