@@ -29,6 +29,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
29
using System ;
30
30
using System . Collections . Generic ;
31
31
using System . Diagnostics ;
32
+ using System . DirectoryServices . AccountManagement ;
32
33
using System . Globalization ;
33
34
using System . IO ;
34
35
using System . Linq ;
@@ -277,22 +278,17 @@ private static string HKLM_GetString(string path, string key)
277
278
278
279
private static void PopulateUsersWindows ( Dictionary < string , bool > newUsers )
279
280
{
280
- // Windows: WMIC
281
- // wmic useraccount get disabled,name
282
- // FALSE username
283
- // TRUE disabledusername
284
- string output = StartProcessAndWait ( "wmic" , "useraccount get disabled,name" ) ;
285
- string [ ] lines = output . Split ( '\n ' ) . Skip ( 1 ) . ToArray ( ) ;
286
- foreach ( string line in lines )
281
+ using var context = new PrincipalContext ( ContextType . Machine ) ;
282
+ using var searcher = new PrincipalSearcher ( new UserPrincipal ( context ) ) ;
283
+
284
+ foreach ( var result in searcher . FindAll ( ) )
287
285
{
288
- string trimmedLine = line . Trim ( ) ;
289
- int pos = trimmedLine . IndexOf ( ' ' ) ;
290
- if ( pos >= 0 )
286
+ if ( result is UserPrincipal userPrincipal )
291
287
{
292
- string disabled = trimmedLine [ .. pos ] . Trim ( ) ;
293
- string foundUserName = trimmedLine [ pos .. ] . Trim ( ) ;
294
- _ = bool . TryParse ( disabled , out bool disabledBool ) ;
295
- newUsers [ foundUserName ] = ! disabledBool ;
288
+ var foundUserName = userPrincipal . SamAccountName ;
289
+ // Treat as enabled if null
290
+ var isAccountEnabled = userPrincipal . Enabled . GetValueOrDefault ( true ) ;
291
+ newUsers [ foundUserName ] = isAccountEnabled ;
296
292
}
297
293
}
298
294
}
0 commit comments