简体   繁体   English

如何在Alias的profile.ps1中向powershell添加命令行参数?

[英]How to add a command line argument to powershell in profile.ps1 for Alias?

I am still quite new to Powershell, but I would like to add my favourite editor into an Alias in Powershell. 我对Powershell还是很陌生,但是我想将我最喜欢的编辑器添加到Powershell中的Alias中。

I edited the profile.ps1 in C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\profile.ps1 which will run automatically when PowerShells starts. 我在C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\profile.ps1中编辑了profile.ps1 ,它将在PowerShell启动时自动运行。

I tried enter New-Alias np notepad.exe which works perfectly everytime I launch PowerShell. 我尝试输入New-Alias np notepad.exe ,它在每次启动PowerShell时都能正常运行。


However, I would like to use Sublime Text 3 as my editor. 但是,我想使用Sublime Text 3作为编辑器。 I followed the instructions in this SO page: How can I write a PowerShell alias with arguments in the middle? 我遵循了此SO页面中的指示: 如何编写中间带有参数的PowerShell别名?

The command line I need for Sublime Text is "C:\\Program Files\\Sublime Text 3\\sublime_text.exe" -n [FirstArg] 我需要Sublime Text的命令行是"C:\\Program Files\\Sublime Text 3\\sublime_text.exe" -n [FirstArg]

Which I come out something like this: function sublime { 'C:\\Program Files\\Sublime Text 3\\sublime_text.exe' -n $args } 我出来的像这样: function sublime { 'C:\\Program Files\\Sublime Text 3\\sublime_text.exe' -n $args }

It does not work and I got the error like this: 它不起作用,我得到了这样的错误:

At C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1:5 char:72
+ ... lime {  'C:\Program Files\Sublime Text 3\sublime_text.exe' -n $args }
+                                                                ~~
Unexpected token '-n' in expression or statement.
At C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1:5 char:75
+ ... lime {  'C:\Program Files\Sublime Text 3\sublime_text.exe' -n $args }
+                                                                   ~~~~~
Unexpected token '$args' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

Any helps would be appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

Without being a Sublime user, I suspect this should work: 如果没有成为Sublime用户,我怀疑这应该可以工作:

function Start-Sublime {
    param([string]$args)

    $limeArgs = [string]::Empty
    if ($args -ne $null) {
        $limeArgs = $args
    }

    Start-Process "$env:ProgramFiles\Sublime Text 3\sublime_text.exe" -n $limeArgs
}

Set-Alias lime Start-Sublime

Not the prettiest PowerShell code, but I imagine it will do what you're after. 不是最漂亮的PowerShell代码,但我想它会做您所追求的。 It's a little easier to understand than the cryptic & operator is. 它比隐式&运算符更容易理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 profile.ps1 如何导入脚本和函数 - profile.ps1 how import scripts and functions PowerShell模块-在何处存储自定义构建的模块以及如何使用profile.ps1导入它们 - PowerShell Modules - Where to store custom built modules and how to import them using profile.ps1 如何找到哪个 profile.ps1 文件用于 PowerShell 集成控制台? - How to find which profile.ps1 file is used for the PowerShell Integrated Console? 如何设置默认位置以在profile.ps1中启动? - How to set default location to start in profile.ps1? AzurePowershell 任务找不到 Profile.ps1 - AzurePowershell Task Cannot Find Profile.ps1 Powershell profile.ps1 无法加载,因为它的操作被软件限制策略阻止 - Powershell profile.ps1 cannot be loaded because its operation is blocked by software restriction policies 如何使用函数在 Powershell 配置文件中为带有选项的命令定义别名? - How to define an alias for a command with options in a Powershell Profile using functions? 如何将命令行参数传递给 PowerShell ps1 文件 - How to pass command-line arguments to a PowerShell ps1 file 如何从命令行在 Powershell ISE 中打开 ps1 脚本? - How can I open a ps1 script in Powershell ISE from the command line? 如何使用PowerShell脚本运行命令行参数 - How to run command line argument using PowerShell script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM