有没有办法以仅在将参数传递给别名的情况下运行的方式为cmdlet创建别名? [英] Is there a way to create an alias to a cmdlet in a way that it only runs if arguments are passed to the alias?

查看:79
本文介绍了有没有办法以仅在将参数传递给别名的情况下运行的方式为cmdlet创建别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建(名为其中)。 core / get-command?view = powershell-6 rel = nofollow noreferrer> Get-Command cmdlet,如果我不发送任何参数,则该cmdlet不会运行(因为如果没有参数将输出所有可用的命令。)

I am trying to create an alias (named which) of the Get-Command cmdlet in a way that it doesn't run if I'm not sending any arguments (because if it's run without arguments it outputs all the commands that are available).

我知道可以使用函数来完成此操作,但是我想保留制表符完成功能而不必编写可放入我的 $ PROFILE

I know this can be done using a function but I would like to keep the tab completion functionality without having to write a sizeable function that is to be placed into my $PROFILE.

简而言之,我只希望别名在传递参数时起作用ts。

In short, I only want the alias to work if it is being passed arguments.

推荐答案

您不能使用别名完成此操作,因为 PowerShell别名只能引用另一个命令 name或path ,因此既不能包含 arguments 也不能包含自定义逻辑

You can't do it with an alias, because PowerShell aliases can only refer to another command name or path, and can therefore neither include arguments nor custom logic.

因此,您确实需要一个功能,但是它可能是一个简短的一个

Therefore you do need a function, but it can be a short and simple one:

function which { if ($args.count) { Get-Command @args } else { Throw "Missing command name." } }

但是请注意,在通过-?用于显示 Get-Command 的帮助确实起作用,参数的制表符补全不起作用。

Note, however, that while passing -? for showing Get-Command's help does work, tab completion of arguments does not.

为了同时完成制表符的完成,您需要编写一个包装器(代理)函数或至少复制 Get-Command 的参数声明-然后使函数定义可调整。

In order to get tab completion as well, you'll need to write a wrapper (proxy) function or at least replicate Get-Command's parameter declarations - which then does make the function definition sizable.

如果关注的只是 $ PROFILE 文件本身,则可以编写代理 script - which.ps1 -其中您也可以仅使用其中进行调用,前提是您将其放置在 $ env:Path 中列出的目录之一中;

If the concern is just the size of the $PROFILE file itself, you can write a proxy script instead - which.ps1 - which you can invoke with just which as well, assuming you place it in one of the directories listed in $env:Path; see next section.

定义包装器(代理)函数或脚本是一项艰巨的任务,但是允许您实现一个强大的包装器,该包装器支持制表符补全,甚至可以转发到原始命令的帮助。

Defining a wrapper (proxy) function or script is a nontrivial undertaking, but allows you to implement a robust wrapper that supports tab completion and even forwarding to the original command's help.

注意:


  • 为简单起见,以下内容创建了一个包装器 script which.ps1 ,并将其保存在当前目录中。如前所述,如果将其放置在 $ env:PATH 列出的目录之一中,则可以像 which

  • For simplicity, the following creates a wrapper script, which.ps1 , and saves it in the current directory. As stated, if you place it in one of the directories listed in $env:PATH, you'll be able to invoke it as just which.

可以轻松地将下面的代码改编为包装器 function :仅取内容的 $ wrapperCmdSource 变量并将其包含在函数中,该函数{...}

The code below can easily be adapted to create a wrapper function instead: simply take the contents of the $wrapperCmdSource variable below and enclose it in function which { ... }.

从PowerShell Core 7.0.0-preview.5开始,自动生成的代码存在一些问题,可能会或不会影响您;他们将被固定在某个时候;要了解更多信息并了解如何手动更正它们,请参见此GitHub问题

As of PowerShell Core 7.0.0-preview.5, there are some problems with the auto-generated code, which may or may not affect you; they will be fixed at some point; to learn more and learn how to manually correct them, see this GitHub issue.

    # Get Get-Command's command's metadata:
$cmdMetaData =
  [System.Management.Automation.CommandMetadata] (Get-Command Get-Command)

    # Create the wrapper scaffolding as source code (outputs a single [string])
    # By default, you'll get comment-based help that forwards to the original cmdlet's help.
    # You can optionally pass a string to serve as the comment-based help as the 2nd argument.
    # By using [System.Management.Automation.ProxyCommand]::GetHelpComments in combination with Get-Help, 
    # you can start with a copy of the original command's help.
$wrapperCmdSource = 
  [System.Management.Automation.ProxyCommand]::Create($cmdMetaData)

# Write the auto-generated source code to a script file
$wrapperCmdSource > which.ps1

您现在拥有一个功能全面的 which.ps1 包装器,其行为类似于 Get-Command 本身。

You now have a fully functional which.ps1 wrapper that behaves like Get-Command itself.

您可以按以下方式调用它:

You can invoke it as follows:

./which  # Same as: Get-Command; tab completion of parameters supported.
./which -? # Shows Get-Command's help.

您现在可以编辑脚本文件以执行所需的自定义。

You can now edit the script file to perform the desired customization.

注意:自动生成的源代码包含很多样板代码;但是,通常只有一两个地方需要调整才能实现自定义功能。

Note: The auto-generated source code contains a lot of boilerplate code; however, typically only one or two places need tweaking to implement the custom functionality.

具体来说,将以下命令作为 begin {...} 块内的第一条语句:

Specifically, place the following command as the first statement inside the begin { ... } block:

if (-not $MyInvocation.ExpectingInput -and -not ($Name -or $CommandType -or $Module -or $FullyQualifiedModule)) {
  Throw "Missing command name or filter."
}

如果调用者未提供某种通过直接参数或通过管道定位特定命令或命令组的方式。

This causes the script to throw an error if the caller didn't provide some way of targeting a specific command or group of commands, either by direct argument or via the pipeline.

如果不带参数调用修改后的脚本现在,您应该会看到所需的错误:

If you invoke the modified script without arguments now, you should see the desired error:

PS> ./which.ps1
Missing command name or filter.
...






其他常见的自定义类型是:


  • 仅通过删除参数声明即可从包装器中删除参数。

  • Removing parameters from the wrapper, by simply removing the parameter declaration.

通过修改开始块中的以下行,在包装命令的调用中添加其他参数:

Adding additional parameters to the invocation of the wrapped command, by modifying the following line in the begin block:

$scriptCmd = {& $wrappedCmd @PSBoundParameters }


  • 在将管道输入传递给包装的命令之前进行预处理,方法是自定义 process 块,并用以下行中的预处理输入替换 $ _

  • Preprocessing pipeline input before passing it to the wrapped command, by customizing the process block and replacing $_ with your preprocessed input in the following line:

    $steppablePipeline.Process($_)
    


  • 这篇关于有没有办法以仅在将参数传递给别名的情况下运行的方式为cmdlet创建别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆