diff --git a/IPBanCore/Core/Utility/OSUtility.cs b/IPBanCore/Core/Utility/OSUtility.cs index 35ab17d6..a5e330a5 100644 --- a/IPBanCore/Core/Utility/OSUtility.cs +++ b/IPBanCore/Core/Utility/OSUtility.cs @@ -29,6 +29,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE using System; using System.Collections.Generic; using System.Diagnostics; +using System.DirectoryServices.AccountManagement; using System.Globalization; using System.IO; using System.Linq; @@ -277,22 +278,17 @@ private static string HKLM_GetString(string path, string key) private static void PopulateUsersWindows(Dictionary newUsers) { - // Windows: WMIC - // wmic useraccount get disabled,name - // FALSE username - // TRUE disabledusername - string output = StartProcessAndWait("wmic", "useraccount get disabled,name"); - string[] lines = output.Split('\n').Skip(1).ToArray(); - foreach (string line in lines) + using var context = new PrincipalContext(ContextType.Machine); + using var searcher = new PrincipalSearcher(new UserPrincipal(context)); + + foreach (var result in searcher.FindAll()) { - string trimmedLine = line.Trim(); - int pos = trimmedLine.IndexOf(' '); - if (pos >= 0) + if (result is UserPrincipal userPrincipal) { - string disabled = trimmedLine[..pos].Trim(); - string foundUserName = trimmedLine[pos..].Trim(); - _ = bool.TryParse(disabled, out bool disabledBool); - newUsers[foundUserName] = !disabledBool; + var foundUserName = userPrincipal.SamAccountName; + // Treat as enabled if null + var isAccountEnabled = userPrincipal.Enabled.GetValueOrDefault(true); + newUsers[foundUserName] = isAccountEnabled; } } } diff --git a/IPBanCore/IPBanCore.csproj b/IPBanCore/IPBanCore.csproj index 14f6cb67..02dd8a25 100644 --- a/IPBanCore/IPBanCore.csproj +++ b/IPBanCore/IPBanCore.csproj @@ -45,6 +45,7 @@ +