Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#相当于此powershell代码段_C#_Wmi - Fatal编程技术网

C#相当于此powershell代码段

C#相当于此powershell代码段,c#,wmi,C#,Wmi,我在PowerShell中有下面的代码片段,它将返回给我远程PC上的本地管理员所需的信息。我正在尝试将此代码转换为c#,但运气很差 $ADMINS = get-wmiobject -computername $computername -Credential $Credential -query "select * from win32_groupuser where GroupComponent=""Win32_Group.Domain='$computername',Name='admin

我在PowerShell中有下面的代码片段,它将返回给我远程PC上的本地管理员所需的信息。我正在尝试将此代码转换为c#,但运气很差

 $ADMINS = get-wmiobject -computername $computername -Credential $Credential -query "select * from win32_groupuser where GroupComponent=""Win32_Group.Domain='$computername',Name='administrators'""" | % {$_.partcomponent}
我可以在c#中使用基本的wmi查询,如下所示: 方法

 public IEnumerable<CimInstance> WmiQuerier(CimSession session , string wmiquery)
        {

            try
            {
                string Namespace = @"root\cimv2";
                IEnumerable<CimInstance> CimInstances = new List<CimInstance>();
                CimInstances = session.QueryInstances(Namespace, "WQL", wmiquery);
                //IEnumerable<CimInstance> CimInstances2 = CimInstances.SelectMany()
                return CimInstances;
            }
            catch (Exception ex )
            {
                Console.WriteLine(ex.Message);

                throw;
            }
   [Test]
        public void CimQuery()
        {
            string querystring = "SELECT * FROM win32_groupuser";
            Wmi wmi = new Wmi();
            NetworkCredential mynetcred = new NetworkCredential("Domain\\User", "Password%");
            CimCredential mycimCred = wmi.ConvertCred(mynetcred);
            CimSession mySession = wmi.WmiConnection(testcomp, mycimCred);
            IEnumerable<CimInstance> querierResults = wmi.WmiQuerier(mySession, querystring).Take(5).ToList();


            Assert.IsInstanceOf<IEnumerable<CimInstance>>(querierResults);
        }
    }
我得到了错误

Microsoft.Management.Infrastructure.CimException:“WS-Management 服务无法处理该请求。WQL查询无效。'


我在WQL字符串中做了哪些错误的操作

由于没有设置
WHERE
子句,您的示例中不清楚它失败的原因。我最好的猜测是,有些角色没有像他们应该的那样逃脱

您还可以使用库为您的问题提供间接解决方案。您可以这样做:

WMIHelper helper = new WMIHelper("root\\CimV2");

var users = helper.Query("SELECT * FROM Win32_GroupUser").ToList().Where(u => u.Contains("Win32_Group.Domain='MachineName',Name='administrators'"));

这里有一个有效的查询。我知道一件事…WQL的语法是非常敏感和不可原谅的…特别是关于空格和引号嵌套。有趣的是,大写/小写都可以:

using System.Management;

//....


      var domainName = "YourDomainName";
      var groupName = "Administrators";
      var wql = string.Format
      (
        @"select partcomponent from win32_groupuser where groupcomponent='Win32_Group.Domain=""{0}"",Name=""{1}""'",
        domainName,
        groupName
      );
      foreach ( var thing in new ManagementObjectSearcher( wql ).Get( ) )
      {
        foreach ( var property in thing.Properties )
        {
          //--> And then, if you want the account object...
          var path = new ManagementPath( property.Value as string );
          var account = new ManagementObject( path );
          foreach ( var acctProp in account.Properties )
          {
            Console.WriteLine( $"{acctProp.Name}={acctProp.Value}" );
          }
        }
      }

Edit:只是为了好玩,我添加了代码来获取引用的Win32#u帐户对象…因为
partcomponent
的值是对该帐户对象的限定引用。

可能不是原因,但您的空格在两种情况下是不同的。您可以从c#调用powershell并以这种方式获取结果。
using System.Management;

//....


      var domainName = "YourDomainName";
      var groupName = "Administrators";
      var wql = string.Format
      (
        @"select partcomponent from win32_groupuser where groupcomponent='Win32_Group.Domain=""{0}"",Name=""{1}""'",
        domainName,
        groupName
      );
      foreach ( var thing in new ManagementObjectSearcher( wql ).Get( ) )
      {
        foreach ( var property in thing.Properties )
        {
          //--> And then, if you want the account object...
          var path = new ManagementPath( property.Value as string );
          var account = new ManagementObject( path );
          foreach ( var acctProp in account.Properties )
          {
            Console.WriteLine( $"{acctProp.Name}={acctProp.Value}" );
          }
        }
      }