private IEnumerable <User> getLocalUsers() { // Get all of the local users. var userAccountQuery = new SelectQuery("Win32_UserAccount"); var userAccountSearcher = new ManagementObjectSearcher(userAccountQuery); var users = userAccountSearcher.Get().Cast <ManagementObject>().Where(x => (bool)x["LocalAccount"]).ToArray(); // Get the SID's of all of the local group users, and the name of the group they're in. var groupUserQuery = new SelectQuery("Win32_GroupUser"); var groupUserSearcher = new ManagementObjectSearcher(groupUserQuery); var groups = new List <Tuple <string, string> >(); foreach (ManagementObject group in groupUserSearcher.Get()) { var pCom = new ManagementObject((string)group["PartComponent"]); var gCom = new ManagementObject((string)group["GroupComponent"]); // Check that this is a local group and the necessary properties exist before adding the user SID and group name. if ((bool)gCom["LocalAccount"] && pCom.GetType().GetProperty("SID") != null && gCom.GetType().GetProperty("Name") != null) { groups.Add(new Tuple <string, string>((string)pCom["SID"], (string)gCom["Name"])); } } //System.Windows.Forms.MessageBox.Show("Users: " + users.Count().ToString() + "\nGroups: " + groups.Count().ToString()); // Get the local users as User objects with their group name set. // There will be duplicates if a user is in more than one group. foreach (var u in users) { var uSID = (string)u["SID"]; var userInGroup = false; foreach (var g in groups) { if (uSID == g.Item1) { yield return(new User(u, g.Item2)); userInGroup = true; } } if (!userInGroup) { yield return(new User(u)); } } }
private void CreateHost(string serverName, string hostName, HostType hostType, string ntGroupName, bool authTrusted, bool HostTracking, bool isHost32BitOnly) { try { PutOptions options = new PutOptions(); options.Type = PutType.CreateOnly; //create a ManagementClass object and spawn a ManagementObject instance ManagementClass objHostSettingClass = new ManagementClass(string.Format(BIZTALKSCOPE, serverName), "MSBTS_HostSetting", null); ManagementObject objHostSetting = objHostSettingClass.CreateInstance(); //set the properties for the Managementobject objHostSetting["Name"] = hostName; objHostSetting["HostType"] = hostType; objHostSetting["NTGroupName"] = ntGroupName; objHostSetting["AuthTrusted"] = authTrusted; objHostSetting["HostTracking"] = HostTracking; objHostSetting["IsHost32BitOnly"] = isHost32BitOnly; Type[] targetTypes = new Type[1]; targetTypes[0] = typeof(PutOptions); object[] parameters = new object[1]; parameters[0] = options; Type objType = objHostSetting.GetType(); MethodInfo mi = objType.GetMethod("Put", targetTypes); mi.Invoke(objHostSetting, parameters); //create the Managementobject //objHostSetting.Put(options); System.Console.WriteLine("Host – " + hostName + " – has been created successfully"); } catch (ManagementException mex) { throw new ApplicationException("Management Exception", mex); } catch (Exception excep) { throw new ApplicationException("CreateHost – " + hostName + " – failed", excep); } }