简体   繁体   English

如何从 Visual Studio 2010 运行 PowerShell 脚本

[英]How do I run a PowerShell script from Visual Studio 2010

如果我在 Visual Studio 中有一个 .ps 文件,我该如何在项目上下文中运行它?

I did this by adding a context menu item linked to an external tool:我通过添加链接到外部工具的上下文菜单项来做到这一点:

Add an "External Tool".添加“外部工具”。 Go to Tools > External Tools.转到工具 > 外部工具。 Add a new one with these settings:使用以下设置添加一个新的:

  • Title: Run with Powershell标题: Run with Powershell
  • Command: powershell.ex命令: powershell.ex
  • Arguments: -ExecutionPolicy RemoteSigned -File "$(ItemPath)"参数: -ExecutionPolicy RemoteSigned -File "$(ItemPath)"
  • Initial Directory: $(ItemDir)初始目录: $(ItemDir)
  • Check " Use Output Window "勾选“ Use Output Window
  • If you plan to use scripts that require arguments, check " Prompt For Arguments "如果您打算使用需要参数的脚本,请选中“ Prompt For Arguments

Take note of the position your tool is in the list (1,2, etc...) Click OK.记下您的工具在列表中的位置(1,2 等...) 单击确定。

  • Now go to Tools > Customize , Commands tab, select Context Menu or Toolbar , and choose "Project and Solution Context Menus | Item".现在转到Tools > CustomizeCommands选项卡,选择Context MenuToolbar ,然后选择“Project and Solution Context Menus | Item”。
  • Click " Add Command... ".单击“ Add Command... ”。
  • Choose Tools category, and choose " External Command X " where x is the position your tool was in the list (index starts with 1, not 0).选择Tools类别,然后选择“ External Command X ”,其中x是您的工具在列表中的位置(索引从 1 开始,而不是 0)。
  • Click OK .单击“ OK
  • Move it to the position you want in the menu, and click "Modify Selection" to give it a friendly name,将它移动到菜单中你想要的位置,然后点击“修改选择”给它一个友好的名字,
  • add keyboard shortcuts, etc..添加键盘快捷键等。
  • Click Close.单击关闭。

Right click your .ps1 file in the solution explorere and enjoy.在解决方案资源管理器中右键单击您的 .ps1 文件并享受。 (NOTE: I also did this for cmd.exe to run .bat files.) (注意:我也为 cmd.exe 执行了此操作以运行 .bat 文件。)

The best way to do this is to install the PowerGUI extension for Visual Studio 2010最好的方法是为 Visual Studio 2010 安装 PowerGUI 扩展

One of the features of this extension is an integrated powershell tool window.此扩展的功能之一是集成的 powershell 工具窗口。 This has access to automation objects like DTE via $EnvDTE and will provide a good environment for running the script against the project这可以通过$EnvDTE访问诸如DTE类的自动化对象,并将为针对项目运行脚本提供良好的环境

Not sure what exactly you mean by "from within the context of the project" but you can:不确定“从项目上下文中”的确切含义,但您可以:

  1. Create a Process创建Process

     Process p = new Process();
  2. Then set the command of the process to be:然后设置进程的命令为:

     powershell.exe YourScriptName.ps1

This works if you just need to run the script.如果您只需要运行脚本,这会起作用。

您还应该查看StudioShell ,它在自定义 PowerShell 主机中提供了 DTE 的深度集成。

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

相关问题 如何关闭从Visual Studio 2010内部运行T-SQL脚本时出现的SQL Server输出子窗口? - How do I close SQL Server output subwindow which appears when I run a T-SQL script from inside Visual Studio 2010? 如何在Visual Studio 2015的C#项目中运行简单的Powershell命令? - How do I run a simple Powershell command from my C# project in Visual Studio 2015? 如何从Visual Studio 2013或2010中的解决方案资源管理器运行sql脚本 - How to run sql script from the solution explorer in Visual Studio 2013 or 2010 如何从Visual Studio运行任意SQLite查询? - How do I run an arbitrary SQLite query from Visual Studio? 如何在Visual Studio 2010 Professional中打开Visual Studio 2010 Express项目? - How do I open a visual studio 2010 express project in visual studio 2010 professional? 如何将VisualSVN集成到Visual Studio 2010中? - How do I integrate VisualSVN into Visual Studio 2010? 如何在Visual Studio 2010中添加数据库图? - How do I add a database diagram in visual studio 2010? 如何在Visual Studio 2010中使用位图调试可视化工具? - How do I use the bitmap debug visualizer in Visual Studio 2010? 如何在Visual Studio 2010中设置Qt路径? - How do I set up Qt paths in Visual Studio 2010? 我如何将Visual Studio 2010升级到2015 - How do i upgrade for Visual studio 2010 to the 2015 one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM