简体   繁体   English

如何从Alias中选择powershell命令并运行该命令

[英]How to select the powershell command from Alias and Run that command

What i want to do is strip the output of the Alis command and get the power-shell command from it and run that command. 我想做的是剥离Alis命令的输出并从中获取power-shell命令并运行该命令。

how i would do this in bash would be i would cut the input and get the second column then store each line in a list. 我将如何在bash中执行此操作,这将是我削减输入并获得第二列,然后将每行存储在列表中。 then get the line that starts with the command i want and use a regular expression to get the seperateor "-> " and then put the result after that in a string and run that command. 然后获取以我想要的命令开头的行,并使用正则表达式获取分隔符“->”,然后将结果放在字符串后并运行该命令。 kinda like this 有点像这样

(if alias worked the same in bash that it does in powershell) (如果别名在bash中的作用与在powershell中相同)

output of alias cd 别名cd的输出

CommandType    Name                  Version           Source
-----------    ----                  -------           ------ 
Alias          cd -> Set-Location

What i want to do is grab the Set location from the command and store that into a string to run the command latter. 我想做的是从命令中获取Set位置,并将其存储到字符串中以稍后运行命令。 the fields is not tab delimited they are space delimited. 字段不是制表符分隔的,而是用空格分隔的。

how i would do this in bash would be i would cut the input and get the second column then store each line in a list. 我将如何在bash中执行此操作,这将是我削减输入并获得第二列,然后将每行存储在列表中。 then get the line that starts with the command i want and use a regular expression to get the seperateor "-> " and then put the result after that in a string 然后获取以我想要的命令开头的行,并使用正则表达式获取分隔符“->”,然后将结果放在字符串后

Fortunately for you, PowerShell cmdlets emit objects instead of text, so it's much easier to extract information like this. 对您来说幸运的是,PowerShell cmdlet发出对象而不是文本,因此提取这样的信息要容易得多。

To get the definition of an alias, select the definition property of the object output by get-alias : 要获取别名的definition ,请通过get-alias选择对象输出的definition属性:

get-alias cd |select-object -expandproperty definition

In short, anytime you start thinking "I'll parse the text output of this cmdlet" in PowerShell, you're probably doing it wrong. 总之, 任何时候你开始思考“我会分析此cmdlet的文本输出”在PowerShell中,你可能就错了。 Instead, pipe the cmdlet to Get-Member and look at the properties and methods hanging off the output object(s). 相反,将cmdlet通过管道传递到Get-Member然后查看挂接输出对象的属性和方法。 Then interrogate those. 然后审问那些。 PowerShell is object-based, not text-based. PowerShell是基于对象的,而不是基于文本的。 If you've been living in bash for a few years/decades, it's an adjustment you have to make. 如果您已经在bash上生活了数十年/数十年,那么您必须进行调整。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM