[英]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:
使用以下设置添加一个新的:
Run with Powershell
Run with Powershell
powershell.ex
powershell.ex
-ExecutionPolicy RemoteSigned -File "$(ItemPath)"
-ExecutionPolicy RemoteSigned -File "$(ItemPath)"
$(ItemDir)
$(ItemDir)
Use Output Window
"Use Output Window
”Prompt For Arguments
"Prompt For Arguments
” Take note of the position your tool is in the list (1,2, etc...) Click OK.记下您的工具在列表中的位置(1,2 等...) 单击确定。
Tools
> Customize
, Commands
tab, select Context Menu
or Toolbar
, and choose "Project and Solution Context Menus | Item".Tools
> Customize
, Commands
选项卡,选择Context Menu
或Toolbar
,然后选择“Project and Solution Context Menus | Item”。Add Command...
".Add Command...
”。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)。OK
.OK
。 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:不确定“从项目上下文中”的确切含义,但您可以:
Create a Process
创建
Process
Process p = new Process();
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.