2017-09-28 31 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",但它運行的是函數本身。

+0

你想寫一個「代理函數」。 [微軟的腳本專家專欄]有一些很好的信息(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

powershell docs&運營商的工作:

# 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

相關問題