Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在powershell中创建别名的最简单方法是什么?_Powershell_Terminal_Windows 10 - Fatal编程技术网

在powershell中创建别名的最简单方法是什么?

在powershell中创建别名的最简单方法是什么?,powershell,terminal,windows-10,Powershell,Terminal,Windows 10,我想知道是否有任何简单的方法可以为像cmd这样的powershell生成别名。 例如:在cmd中,doskey art=php artisan$*其中$*是可选的。目前,我在powershell中使用以下别名 function runArtisanCommand { param( [Parameter(Mandatory=$false, Position = 0, ValueFromRemainingArguments = $true)] $command

我想知道是否有任何简单的方法可以为像cmd这样的powershell生成别名。 例如:在cmd中,
doskey art=php artisan$*
其中
$*
是可选的。目前,我在powershell中使用以下别名

function runArtisanCommand
{
    param(
        [Parameter(Mandatory=$false, Position = 0, ValueFromRemainingArguments = $true)]
        $command
    )
    php artisan $command
}

Set-Alias art runArtisanCommand
这在某种程度上起作用,但不带标志。例如:我不能写
art-h
art-route:list-c
。在
art-h
命令中,它打印
php artisan
的输出,并且根本不读取标志,但在
art-route:list-c
命令中,它会出错

runArtisanCommand : Missing an argument for parameter 'command'. Specify a parameter of type 'System.Object' and try again.
At line:1 char:16
+ art route:list -c
+                ~~
    + CategoryInfo          : InvalidArgument: (:) [runArtisanCommand], ParameterBindingException
    + FullyQualifiedErrorId : MissingArgument,runArtisanCommand

我想要一个比这更简单的解决方案。提前感谢。

传递未知参数最简单、最方便的方法是 简单函数或脚本中的-as
@args
(既不使用
[CmdletBinding()]
属性,也不使用
[Parameter()]
属性的函数或脚本):

#注意:@args而不是$args使函数与命名
#PowerShell命令的参数也有-请参阅下面的说明。
函数runArtisan命令{php artisan@args}
#与您的问题一样:为函数定义别名“art”
#注意:当然,您可以直接将*函数*命名为'art'。
如果你想让函数有一个较长的名字,请考虑一个
#遵循PowerShell的动词-名词命名约定,例如
#“调用命令”。
设置Alias art RunArtism命令
另一方面:由于目标可执行文件
php
,既没有基于变量或表达式引用也没有指定,因此可以按原样调用它;否则,您将需要
&
,有关背景信息,请参阅


至于你所尝试的:

问题是使用
-c
作为传递参数仅在前面加上
--
时有效:

#好的,多亏了“-”
艺术——路线:列表——c
--
告诉PowerShell将所有剩余的参数视为未命名(位置)参数,而不是尝试将标记(如
-c
)解释为参数名称

如果没有
--
-c
将被解释为引用您的
-command
参数(您声明为
$command
的参数带有
ValueFromRemainingArguments=$true
),因为PowerShell允许您指定名称前缀而不是完整的参数名,只要给定的前缀是明确的

由于除
[switch]
以外的任何类型的参数都需要关联的参数,
-c
(又称
-command
)失败,并显示相应的错误消息

您可以通过命名参数来避免冲突,使其不会与任何传递参数冲突,例如将其命名为
-\u args
(使用参数变量
$\u args
):

但是,如果使用
[参数()]
属性会隐式地使函数成为函数,那么它也会始终接受,例如
-ErrorAction
-OutVariable
-Verbose
,所有这些都可以通过明确的前缀/短别名传递;e、 例如,
-outv
表示
-OutVariable
,或别名
-ea
表示
错误动作
无法避免与它们的碰撞

因此,诸如
-e
之类的预期传递参数仍然不起作用:

#失败,因为-e与公共参数-ErrorAction模糊匹配
#和-错误变量。
PS>艺术路由器:列表-e
无法处理参数,因为参数名称“e”不明确。
可能的匹配项包括:-ErrorAction-ErrorVariable。
同样,需要使用
--

#好的,多亏了“-”
艺术——路由器:列表——e

摘要

  • 特别是对于函数,将调用包装到外部程序,例如
    php.exe
    ,使用带有
    @args
    的简单函数,如顶部所示,不仅更简单,而且更健壮

  • 对于包装PowerShell命令的函数(带有明确声明的参数):

    • 带有
      @args
      的简单函数也可以工作
    • <> LI>但如果您还需要对Tabor完成支持< <强> >和<强>使用支持的参数显示< /强>,通过“代码> > -/CODE >,或通过,考虑通过PosithSek SDK<强>定义一个(总是高级的)<强>代理(包装器)函数-见下面/

可选背景信息:在PowerShell中传递参数

正如所指出的,将传递给函数或脚本的(未声明的)参数传递给另一个命令的最简单方法是使用,它是传递给简单函数或脚本的所有参数的自动填充数组(通过使用
[CmdletBinding()]
和/或
[参数()]
属性)

至于为什么应该使用
@args
(splatting)而不是
$args

  • 在包装器函数中使用
    $args
    仅适用于传递位置参数(没有参数名称前缀的参数;例如
    *.txt
    ),而不是命名参数(例如
    -Path*.txt

  • 如果最终目标命令是一个外部程序(如本例中的
    php.exe
    ),这不是问题,因为PowerShell of Essential会将所有参数视为位置参数(它无法知道目标程序的语法)

  • 但是,如果最终调用了PowerShell命令(带有正式声明的参数),则只调用
    $args
    数组,这在语法上意味着我们使用
    @args
    -
    function runArtisanCommand
    {
        param(
            # Note: `Mandatory = $false` and `Position = 0` are *implied*.
            [Parameter(ValueFromRemainingArguments)]
            $_args
        )
        php artisan @_args
    }
    
    # Simple wrapper function for Get-ChildItem that lists recursively
    # and by relative path only.
    function dirTree {
      # Use @args to make sure that named arguments are properly passed through.
      Get-ChildItem -Recurse -Name @args 
    }
    
    # Invoke it with a *named* argument passed through to Get-ChildItem
    # If $args rather than @args were used inside the function, this call would fail.
    dirTree -Filter *.txt