[英]Wrapper function with same name as command in Powershell?
I am making attempting to fix an app which has recently broken the ls
command, using a Powershell wrapper script. 我正在尝试使用Powershell包装器脚本修复最近破坏了ls
命令的应用程序。 My wrapper must have the same name as the command it is invoking, so I'm using invoke-command
to call the original command. 我的包装器必须与正在调用的命令具有相同的名称,因此我正在使用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
}
However invoke-command
fails with 但是, invoke-command
失败,并且
Invoke-Command : Parameter set cannot be resolved using the specified named parameters
How can I run the original command with the modified parameters? 如何使用修改后的参数运行原始命令?
Edit: I've also tried -ArgumentList (,$modifiedArgs)
per Passing array to another script with Invoke-Command and I still get the same error. 编辑:我也尝试了-ArgumentList (,$modifiedArgs)
每个数组传递给另一个脚本与Invoke-Command ,我仍然遇到相同的错误。
Edit: Looks like invoke-command
is remoting only. 编辑:看起来像invoke-command
只是远程。 I've also tried Invoke-Expression "& yarn $modifiedArgs"
but that runs the function itself. 我也尝试了Invoke-Expression "& yarn $modifiedArgs"
但是它本身运行函数。
Per the powershell docs the &
operator works: 根据powershell文档 , &
运算符可以工作:
# 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
}
Still happy to accept other answers that don't involve specifying the command path explicitly. 仍然很乐意接受其他不涉及明确指定命令路径的答案。
I belive you can get around needing to specify the full path to the command if you scope the wrapper function to Private: 我相信,如果将包装函数的作用域限定为私有,则可以指定命令的完整路径:
# yarn broke 'ls'
function Private:yarn() {
$modifiedArgs = @()
foreach ( $arg in $args ) {
if ( $arg -cmatch '^ls$' ) {
$arg = 'list'
}
$modifiedArgs += $arg
}
& yarn $modifiedArgs
}
This will prevent the function from being visible to child scopes, and it will revert back to using the application. 这将防止该功能在子作用域中可见,并且将恢复使用该应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.