在 Visual Studio 中查看 PowerShell 的 Select-String 输出 [英] Viewing PowerShell's Select-String output in Visual Studio

查看:73
本文介绍了在 Visual Studio 中查看 PowerShell 的 Select-String 输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在 Visual Studio 中使用 PowerShell 的 Select-String 函数.

我写了一篇关于如何查看的博客文章Notepad++ 中的 Select-String 输出利用 NppExec 调用外部程序的能力,以及它添加自定义行模式匹配以匹配 ::<;行内容>.

在 Visual Studio 中有没有办法做到这一点,也许是通过将文本吐出到输出窗口?

解决方案

VS 确实会解释输出窗口中的文本,如果发现类似

():

双击该行导航到该文件中给定的行号,将 Select-String 的输出转换为这种特定格式没什么大不了的.

看你的博文,我有点喜欢这个想法(+1)但我不太热衷于工作流程:假设我在 VS 我真的不想切换到控制台,然后输入文本,然后切换回 VS 并执行一些命令.然后我宁愿只使用现有的在文件中查找功能,最终使用正则表达式.几乎相同,更快,更方便.

然而,总是有方便的自动化空间,我肯定看到你的想法有一些用途,所以这里有一个例子,说明如何利用 PS 的 dir | 的 External Tools 功能.sls 在当前文件所在目录下的所有文件中查找当前选中文本的所有匹配项:

  • 在 VS 中,选择 Tools->External Tools...
  • 选择添加,设置合适的Title,设置powershell.execommand
  • 参数设置为<代码>-NoProfile -Command dir -r $(ItemDir) |Select-String -simplematch '$(CurText)' |ForEach-Object { '{0}({1}): {2}' -f $_.Path, $_.LineNumber, $_.Line }
  • 勾选使用输出窗口,点击确定

现在在 VS 中打开一个文件并选择文本,然后选择 Tools->YourCommandTitle 并双击任何匹配项(因为当前文件包含在搜索中,所以至少会有一个).>

根据口味调整命令(查看此处了解更多内置内容-在参数中),为其分配一个键盘快捷键,然后就可以了!

I am looking to use PowerShell's Select-String function within Visual Studio.

I have written a blog post about how to view Select-String output within Notepad++ taking advantage of NppExec's ability to call an external program, and its ability to add a custom line pattern matching to match <path>:<line-number>:<line-content>.

Is there any way of doing this within Visual Studio, perhaps by spitting out text to the Output Window?

解决方案

VS indeed interprets text in the output window and if it finds something like

<filename>(<linenumber>):

doubleclicking that line navigates to the given line number in that file, and it's no big deal transforming the output of Select-String to this particluar format.

Looking at your blog post, I kinda like the idea (+1 for that) but I'm not too keen on the workflow: suppose I'm in VS I don't really want to switch to the console, then type text, then switch back to VS and execute some command. Then I'd rather just use the existing Find in Files functionality, eventually with regular expressions. Does pretty much the same, faster and handier.

However there's always room for handy automation and I definitely see some uses for your idea so here's an example of how to leverage the External Tools funcionality with PS's dir | sls to look for all matches of the currently selected text in all files in the directory of the current file:

  • in VS, select Tools->External Tools...
  • select Add, set suitable Title, set powershell.exe as command
  • set the Arguments to -NoProfile -Command dir -r $(ItemDir) | Select-String -simplematch '$(CurText)' | ForEach-Object { '{0}({1}): {2}' -f $_.Path, $_.LineNumber, $_.Line }
  • check Use Output window, click Ok

Now open a file in VS and select text, then select Tools->YourCommandTitle and doubleclick any matches (there will be at least one since the current file is included in the search).

Adjust the command to taste (look here for more built-in arguments), assign a keyboard shortcut to it and off you go!

这篇关于在 Visual Studio 中查看 PowerShell 的 Select-String 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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