Skip to content

Commit

Permalink
Replace wmic for user account info on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
jjxtra committed Sep 10, 2024
1 parent 3223c6b commit d62b8ab
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
24 changes: 10 additions & 14 deletions IPBanCore/Core/Utility/OSUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -277,22 +278,17 @@ private static string HKLM_GetString(string path, string key)

private static void PopulateUsersWindows(Dictionary<string, bool> 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;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions IPBanCore/IPBanCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.3.2" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="8.0.0" />
<PackageReference Include="System.DirectoryServices.AccountManagement" Version="8.0.0" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="System.Threading.Tasks.Dataflow" Version="8.0.0" />
<PackageReference Include="TaskScheduler" Version="2.11.0" />
Expand Down

0 comments on commit d62b8ab

Please sign in to comment.