【发布时间】:2017-09-28 12:48:04
【问题描述】:
我正在尝试使用 Powershell 包装脚本修复最近破坏了ls 命令的应用程序。我的包装器必须与它正在调用的命令同名,所以我使用invoke-command 来调用原始命令。
# 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) per
Passing array to another script with Invoke-Command 我仍然得到同样的错误。
编辑:看起来invoke-command 只是远程处理。我也尝试过Invoke-Expression "& yarn $modifiedArgs",但它会运行函数本身。
【问题讨论】:
-
你想写一个“代理函数”。在Microsoft's Scripting Guy column、Microsoft TechNet's Script Center Repository 和PowerShell With a Purpose (Windows ITPro) 上有一些很好的信息。如果您想了解更多信息,this Google search 可能会有用。
-
感谢@JeffZeitlin,但这是一个二进制命令:我没有包装其他 powershell。即使是这样,根据脚本专家专栏提取原始函数的代码似乎在这里用大锤敲碎核桃。
标签: powershell