简体   繁体   English

包装功能与Powershell中的命令同名吗?

[英]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.

相关问题 如何运行与 PowerShell 别名同名的命令? - How to run a command with the same name as a PowerShell alias? PowerShell 中的包装函数:传递剩余参数 - Wrapper function in PowerShell: Pass remaining parameters 接受具有相同名称但值不同的多个参数的PowerShell函数 - PowerShell function that accepts multiple parameters with the same name, but different values Powershell 命令复制具有特定名称的文件并粘贴到与当前日期名称相同的文件夹中 - Powershell command copies files with certain name and paste in a folder with the name same as current date PowerShell - 使用与函数相同的名称覆盖该 cmdlet 的名称时,如何在函数中调用该 cmdlet? - PowerShell - How do I call a cmdlet in a function when overriding that cmdlet's name with the same name as the function? 直接在powershell命令提示符下执行powershell函数 - Execute a powershell function directly in powershell command prompt Powershell将功能传递给远程命令 - Powershell Passing Function to remote command Powershell 在函数中迭代变量名 - Powershell iterate variable name in function 获取Powershell中调用函数的名称 - Get name of invoking function in Powershell 如何编写一个wrapper powershell命令,将所有参数转发给委托命令? - How do I write a wrapper powershell command that forwards all arguments to a delegate command?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM