PowerShell 新手问题:
我想在 PowerShell 中创建一个与此 Bash 别名完全相同的别名:
alias django-admin-jy="jython /path/to/jython-dev/dist/bin/django-admin.py"
到目前为止,在修补它的过程中,我发现这非常困难。
-PowerShell别名仅适用于PowerShell命令+函数调用
- 没有明确的方法来允许 PowerShell 函数调用上无限数量的参数
-PowerShell 似乎阻止了标准输出
值得注意的是,我已经尝试过这里提出的解决方案:http://huddledmasses.org/powershell-power-user-tips-bash-style-alias-command/ http://huddledmasses.org/powershell-power-user-tips-bash-style-alias-command/
在加载 PowerShell 时出现以下与语法相关的错误:
The term 'which' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spell
ing of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\Dan\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:9 char:27
+ $cmd = @(which <<<< $_.Content)[0]
+ CategoryInfo : ObjectNotFound: (which:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
PowerShell 别名不允许论点.
They 只能引用一个命令name,它可以是 cmdlet 或函数的名称,也可以是脚本或可执行文件的名称/路径。
为了得到你所追求的东西,你需要定义一个function:
function django-admin-jy {
jython.exe /path/to/jython-dev/dist/bin/django-admin.py @args
}
这使用了自 PowerShell 2.0 以来可用的功能,称为参数飞溅 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting: 可以申请@
引用数组或哈希表的变量名。
在这种情况下,我们将其应用到自动变量 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables named args
,其中包含所有参数(未绑定到显式声明的参数 - see about_函数 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions).
如果您想要一种真正通用的方法来创建带有参数的别名函数,请尝试以下操作:
function New-BashStyleAlias([string]$name, [string]$command)
{
$sb = [scriptblock]::Create($command)
New-Item "Function:\global:$name" -Value $sb | Out-Null
}
New-BashStyleAlias django-admin-jy 'jython.exe /path/to/jython-dev/dist/bin/django-admin.py @args'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)