使用c#代码调用powershell

在c#中调用powershell命令,本文介绍了两种方法,一种是在运行的过程中打开powershell界面,另一种是不打开界面,直接运行命令

目录

预备知识:使用c#打开.exe文件

一、在运行的过程中打开powershell界面

二、 不打开界面,直接运行命令

参考资料:


预备知识:使用c#打开.exe文件

using System.Diagnostics;

Process p = Process.Start("powershell.exe");
p.start();

一、在运行的过程中打开powershell界面

	using System.Linq;
	using System.Management.Automation;
	using System.Text;
	using System.Threading.Tasks;
	
	namespace testCentennial
	{
	    class Program
	    {
	        static void Main(string[] args)
	        {
	            var process = new Process();	           
	            process.StartInfo.FileName = @"powershell.exe";
	            process.StartInfo.Arguments = "get-process"; 
	            process.Start();
	        }
	    }
	}

(1) 如果只添加一条命令的话,可以直接把命令添加到

process.StartInfo.Arguments = "get-process"; 

(2) 如果想要在powershell运行完命令后不关闭界面,可以把上面那条代码写为:

 process.StartInfo.Arguments = "-noexit get-process";

(3) 如果想要添加.ps1脚本的话,

 static void Main(string[] args)
	        {
	            File.GetAttributes("Start.ps1");
	            string strCmdText = Path.Combine(Directory.GetCurrentDirectory(), "Start.ps1");
	            var process = new Process();	   
	            process.StartInfo.FileName = @"powershell.exe";//也可以打开自己写的.exe文件,加上路径和名字即可
	            process.StartInfo.Arguments = "-noexit \"&'" + strCmdText + "'\"";	           
	            process.Start();
	   }

 (4)如果想重定向输出,将powershell获得的结果输出到文本中

 static void Main(string[] args)
	        {
	            File.GetAttributes("Start.ps1");
	            string strCmdText = Path.Combine(Directory.GetCurrentDirectory(), "Start.ps1");
	            var process = new Process();
	   
	            process.StartInfo.UseShellExecute = false;
	            process.StartInfo.RedirectStandardOutput = true;	         
	            process.StartInfo.FileName = @"powershell.exe";//也可以打开自己写的.exe文件,加上路径和名字即可
	            process.StartInfo.Arguments = "-noexit \"&'" + strCmdText + "'\"";	           
	            process.Start();
	   
	            string s = process.StandardOutput.ReadToEnd();
	            process.WaitForExit();
	
	            using (StreamWriter outfile = new StreamWriter("StandardOutput.txt", true))
	            {
	                outfile.Write(s);
	            }
	        }

关于Process.startInfo 属性的具体介绍,可以查看:

https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.startinfo?view=netcore-3.1

 

二、 不打开界面,直接运行命令

using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {                      
            Runspace runspace = RunspaceFactory.CreateRunspace();
            runspace.Open();

            Pipeline pipeline = runspace.CreatePipeline();
            pipeline.Commands.AddScript("想要添加的命令");
            pipeline.Commands.AddScript("想要添加的命令");
            pipeline.Invoke();
            runspace.Close();
            
        }
    }
}

参考资料:

https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces?view=powershellsdk-7.0.0

https://docs.microsoft.com/en-us/powershell/scripting/developer/hosting/adding-and-invoking-commands?view=powershell-7

  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值