C#中的Powershell命令 [英] Powershell Command in C#

查看:72
本文介绍了C#中的Powershell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查询 root\CIMV2 命名空间中所有 WMI 类的名称.有没有办法在 C# 中使用 powershell 命令来检索此信息?

I am trying to query the names all of the WMI classes within the root\CIMV2 namespace. Is there a way to use a powershell command to retrieve this information in C# ?

推荐答案

沿用 Keith 的方法

Along the lines of Keith's approach

using System;
using System.Management.Automation;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var script = @" 
                Get-WmiObject -list -namespace root\cimv2 | Foreach {$_.Name}
            ";

            var powerShell = PowerShell.Create();
            powerShell.AddScript(script);

            foreach (var className in powerShell.Invoke())
            {
                Console.WriteLine(className);
            }
        }
    }
}

这篇关于C#中的Powershell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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