title: WMI Folder Add ACL Type Mismatch
body: <p>I am attempting to write a small application to update folder permissions. I have written the following code to remove <code>oldGroup</code> and add in <code>newGroup</code>.</p>
  
  <p>When I call <code>InvokeMethod</code> I catch an exception <code>HRESULT 0x80041005 - Type Mismatch</code>. Not very helpful! If I comment out <code>newAces.Add(newAce);</code> the old group is successfully removed so the issue lies in my new ACE (<code>newAce</code>) or trustee (<code>trustee</code>). I have tried several methods of instantiating <code>trustee</code>, commented out below.</p>
  
  <pre><code>public void Function()
      {
          CimInstance QueryInstance(CimSession session, string cimNamespace, string query)
          {
              IEnumerable&lt;CimInstance&gt; queryInstances = session.QueryInstances(cimNamespace, "WQL", query);
              return queryInstances.FirstOrDefault();
          }
          string computerName = "localhost";
          string namespaceName = @"root\cimv2";
          string oldGroup = "Everyone";
          string newGroup = "Not Everyone";
          DComSessionOptions sessionOptions = new DComSessionOptions
          {
              Timeout = new TimeSpan(0, 2, 0)
          };
          CimSession cimSession = CimSession.Create(computerName, sessionOptions);
  
          CimInstance trustee = new CimInstance(cimSession.GetClass(namespaceName, "Win32_Trustee"));
          //CimInstance trustee = new CimInstance("Win32_Trustee");
          trustee.CimInstanceProperties.Single(p =&gt; p.Name == "Name").Value = newGroup;
          //trustee.CimInstanceProperties.Add(CimProperty.Create("Name", newGroup, CimType.String, CimFlags.Key));
          trustee.CimInstanceProperties.Single(p =&gt; p.Name == "Domain").Value = "GLOBAL";
          //trustee.CimInstanceProperties.Add(CimProperty.Create("Domain", "GLOBAL", CimType.String, CimFlags.Key));
  
          CimInstance newAce = new CimInstance("Win32_ACE");
          newAce.CimInstanceProperties.Add(CimProperty.Create("AccessMask", 1179817, CimFlags.Key));
          newAce.CimInstanceProperties.Add(CimProperty.Create("AceFlags", 3, CimFlags.Key));
          newAce.CimInstanceProperties.Add(CimProperty.Create("AceType", 0, CimFlags.Key));
          newAce.CimInstanceProperties.Add(CimProperty.Create("Trustee", trustee, CimFlags.Key));
  
          CimInstance logicalFileSecSetting = QueryInstance(cimSession, namespaceName, @"select * from Win32_LogicalFileSecuritySetting where Path='C:\\dev\\temp\\wmi'");
          CimMethodResult methodResult;
          methodResult = cimSession.InvokeMethod(namespaceName, logicalFileSecSetting, "GetSecurityDescriptor", new CimMethodParametersCollection());
          CimInstance descriptor = (CimInstance)methodResult.OutParameters.SingleOrDefault(p =&gt; p.Name == "Descriptor").Value;
          IEnumerable&lt;CimInstance&gt; aces = (IEnumerable&lt;CimInstance&gt;)descriptor.CimInstanceProperties.SingleOrDefault(p =&gt; p.Name == "DACL").Value;
          List&lt;CimInstance&gt; newAces = aces.Where(ace =&gt;
          {
              CimInstance aceTrustee = (CimInstance)ace.CimInstanceProperties.Single(p =&gt; p.Name == "Trustee").Value;
              string aceTrusteeName = (string)aceTrustee.CimInstanceProperties.Single(p =&gt; p.Name == "Name").Value;
              return aceTrusteeName != oldGroup;
          }).ToList();
  
          newAces.Add(newAce);
          descriptor.CimInstanceProperties.SingleOrDefault(p =&gt; p.Name == "DACL").Value = newAces.ToArray();
  
          CimInstance cimDirectory = QueryInstance(cimSession, namespaceName, @"SELECT * FROM Win32_Directory WHERE Name='C:\\dev\\temp\\wmi'");
          CimMethodParametersCollection methodParameters = new CimMethodParametersCollection
          {
              CimMethodParameter.Create("SecurityDescriptor", descriptor, CimType.Instance, CimFlags.In),
              CimMethodParameter.Create("Option", 4, CimType.UInt32, CimFlags.In)
          };
          methodResult = cimSession.InvokeMethod(namespaceName, cimDirectory, "ChangeSecurityPermissions", methodParameters);
      }
  </code></pre>
  
  <p>Could anyone more familiar with the Microsoft Management Infrastructure help me out? Thanks in advance.</p>