public static CommandResult InvokeWmiMethod(string wmiNamespace, string wmiClass, string methodName, string methodArguments, string computerName, string username, string password) { CommandResult invokeResults = new CommandResult(1); ManagementScope scope = GetScope(wmiNamespace, computerName, username, password); ManagementClass mgmtClass = new ManagementClass(scope.Path.Path, wmiClass, null); MethodData method = mgmtClass.Methods[methodName]; // -1 because ReturnValue does not count int paramCount = method.InParameters.Properties.Count + method.OutParameters.Properties.Count - 1; // Invoke the method object[] methodArgs = new object[paramCount]; methodArgs[0] = methodArguments; // TODO, it should be possible to provide more arguments object returnValue = mgmtClass.InvokeMethod(methodName, methodArgs); // Store the ReturnValue ResultRecord outParams = new ResultRecord(); outParams.Add("ReturnValue", Convert.ToString(returnValue)); // Store other outParams int i = method.InParameters.Properties.Count; foreach (PropertyData param in method.OutParameters.Properties) { // ReturnValue is not stored in the methodarguments, // but instead is just the return value of the InvokeMethod method if (param.Name == "ReturnValue") { continue; } outParams.Add(param.Name, Convert.ToString(methodArgs[i])); i++; } invokeResults.Add(outParams); return(invokeResults); }
public static CommandResult QueryLDAP(string queryFilter, List <string> properties) { CommandResult _results = new CommandResult(); DirectoryEntry entry = new DirectoryEntry(); using (DirectorySearcher ds = new DirectorySearcher(entry)) { // Setup properties ds.PropertiesToLoad.Clear(); ds.PropertiesToLoad.AddRange(properties.ToArray()); // Filter ds.Filter = queryFilter; // Perform query SearchResultCollection results = ds.FindAll(); // Store results for (int i = 0; i < results.Count; i++) { SearchResult result = results[i]; // First records should have the same number of properties as any other record ResultRecord foundUser = new ResultRecord(results[0].Properties.Count); // Iterate over result properties foreach (DictionaryEntry property in result.Properties) { string propertyKey = property.Key.ToString(); ResultPropertyValueCollection objArray = (ResultPropertyValueCollection)property.Value; // Fixups switch (propertyKey.ToLowerInvariant()) { // The UserAccountControl bitmask contains a bit which states whether the account is enabled or not case "useraccountcontrol": foundUser.Add("Enabled", IsActive(result).ToString()); continue; // Byte array needs to be converted to GUID case "objectguid": Guid g = new Guid((byte[])objArray[0]); foundUser.Add(propertyKey, g.ToString()); continue; // Byte array needs to be converted to SID case "objectsid": SecurityIdentifier sid = new SecurityIdentifier((byte[])objArray[0], 0); foundUser.Add(propertyKey, sid.ToString()); continue; // This attribute is automatically added case "adspath": if (!properties.Contains(propertyKey)) { continue; } break; } // Convert objects to string string[] strArray = new string[objArray.Count]; for (int j = 0; j < objArray.Count; j++) { strArray[j] = objArray[j].ToString(); } // Concatenate strings string strValue = string.Join(";", strArray); foundUser.Add(property.Key.ToString(), strValue); } _results.Add(foundUser); } } return(_results); }
public static CommandResult ExecuteWmiQuery(string wmiNamespace, string wmiQuery, string computerName, string username, string password) { CommandResult queryResults = null; ManagementScope scope = GetScope(wmiNamespace, computerName, username, password); ObjectQuery query = new ObjectQuery(wmiQuery); ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); ManagementObjectCollection queryCollection = searcher.Get(); Dictionary <string, int> columns = new Dictionary <string, int>(StringComparer.InvariantCultureIgnoreCase); queryResults = new CommandResult(queryCollection.Count); // Determine column order int start = wmiQuery.ToLower().IndexOf("select") + "select".Length; int length = wmiQuery.ToLower().IndexOf(" from") - start; string columns_string = wmiQuery.Substring(start, length); string[] dirty_columns = columns_string.Split(','); foreach (string col in dirty_columns) { columns.Add(col.Trim(), col.Trim().Length); } // Case of SELECT * bool wildCardSelect = false; if (columns.ContainsKey("*")) { columns = new Dictionary <string, int>(StringComparer.InvariantCultureIgnoreCase); wildCardSelect = true; } // Collect data foreach (ManagementObject m in queryCollection) { ResultRecord result = new ResultRecord(m.Properties.Count, StringComparer.InvariantCultureIgnoreCase); // Case of SELECT * if (wildCardSelect) { foreach (PropertyData data in m.Properties) { columns.Add(data.Name, data.Name.Length); } wildCardSelect = false; } // Prepare order of columns foreach (string column in columns.Keys) { result.Add(column, null); } // Collect attributes foreach (PropertyData data in m.Properties) { string key = data.Name; string value = string.Empty; if (data.Value != null) { if (data.Value.GetType() == typeof(string[])) { value = string.Join(", ", (string[])data.Value); } else { value = Convert.ToString(data.Value); } } result[key] = value; } queryResults.Add(result); } return(queryResults); }