简体   繁体   English

如何在Visual Studio 2015的C#项目中运行简单的Powershell命令?

[英]How do I run a simple Powershell command from my C# project in Visual Studio 2015?

I have a simple one line Powershell command which unblocks all dlls in a specific folder. 我有一个简单的单行Powershell命令,它可以解除阻止特定文件夹中的所有dll。 I want to run this command from my C# main() method in VS 2015. 我想从VS 2015中的C# main()方法运行此命令。

I have tried using Runspace but VS is not recognizing it. 我尝试使用运行Runspace但VS无法识别它。

How can I do this ? 我怎样才能做到这一点 ? Any extensions that I might have to install ? 我可能必须安装任何扩展程序吗?

试试下面的过程, Process.Start应该根据需要执行。

System.Diagnostics.Process.Start("Path/To/Powershell/Script.ps1");

First off, I love the question. 首先,我喜欢这个问题。 I'm a serious PowerShell fan and love to figure out something new about PowerShell almost every day. 我是PowerShell的忠实粉丝,几乎每天都想了解有关PowerShell的新知识。

Now, for the answer. 现在,为答案。

Here's what I'm going to do. 这就是我要做的。 First, I'm going to open up PowerShell, without making the window show up. 首先,我将打开PowerShell,而不会显示该窗口。 Then, I'm going to run the Get-Process command, since it provides some good info. 然后,我将运行Get-Process命令,因为它提供了一些不错的信息。 Finally, I'm going to print the results to a screen, then wait for the user to press any key to verify that they saw the response. 最后,我将结果打印到屏幕上,然后等待用户按任意键来验证他们是否看到了响应。 (If you want it in a string, look into StringBuilder.) This will basically do what you asked; (如果要在字符串中使用它,请查看StringBuilder。)这基本上可以满足您的要求; run a single, simple command, and get the output. 运行一个简单的命令,并获取输出。

Here's the code. 这是代码。

using System;
using System.Diagnostics;

namespace powershellrun {
    public class program {
        public static void Main(string[] args) {
            //Open up PowerShell with no window
            Process ps = new Process();
            ProcessStartInfo psinfo = new ProcessStartInfo();
            psinfo.FileName = "powershell.exe";
            psinfo.WindowStyle = ProcessWindowStyle.Hidden;
            psinfo.UseShellExecute = false;
            psinfo.RedirectStandardInput = true;
            psinfo.RedirectStandardOutput = true;
            ps.StartInfo = psinfo;
            ps.Start();
            //Done with that.

            //Run the command.
            ps.StandardInput.WriteLine("Get-Process");
            ps.StandardInput.Flush();
            ps.StandardInput.Close();
            ps.WaitForExit();
            //Done running it.

            //Write it to the console.
            Console.WriteLine(ps.StandardOutput.ReadToEnd());
            //Done with everything.

            //Wait for the user to press any key.
            Console.ReadKey(true);
        }
    }
}

This should do the job for you. 这应该为您完成工作。

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

相关问题 如何在Visual Studio 2015中禁用C#6支持? - How do I disable C# 6 Support in Visual Studio 2015? 如何在Visual Studio 2015输出窗口中阻止C#编译器输出? - How do I stop the C# compiler output from wrapping in the Visual Studio 2015 output window? 从 Visual Studio 2015 无效清单发布 C# 项目 - Publishing C# project from Visual Studio 2015 invalid manifest Visual Studio 2015 C# 缩短命令 - Visual studio 2015 C# Shorten command 如何为我的 Visual Studio 2010 C# 项目制作安装程序? - How do I make an installer for my Visual Studio 2010 C# project? 如何在Visual Studio C中创建“项目概述”# - How do I create a “Project overview” in Visual Studio C# (Visual Studio) 如何将 powershell 脚本导入 c# 项目,以便我可以将 .NET 应用程序与脚本一起发布 - (Visual Studio) How to import powershell scripts into c# project so that I can publish my .NET application together with the scripts 如何将报表添加到Visual Studio 2015项目面板? - How do I add reporting to my Visual Studio 2015 project panel? 如何从Visual Studio中找到c#项目的类型? - How do I find type of c# project from Visual Studio? 如何在.NET Core,C#6和Visual Studio 2015中使用本地IIS? - How do I use a Local IIS with .NET Core, C# 6 and Visual studio 2015?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM