Mike
Mike

Reputation: 39

Unable to obtain status (Disable) of Local user

I ‘m trying to obtain a list of Local Users with Group Membership and Disabled status. I had some success however could not get the corresponding Disable status of each local user. I’m using WMI in conjunction with ADSI to obtain a Disable Status of local users. The Disable Status property is not available with ADSI. Since, I’m a PS newbie any alternative options or explanation would be greatly appreciated. FYI: These are standalone servers not member servers

$adsi = [ADSI]"WinNT://$env:computername"
$user = $adsi.Children | where {$_.SchemaClassName -eq 'user'}
$user| Foreach-Object {
    $g1 = $_.Groups() | Foreach-Object {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
    $s1 = gwmi -ClassName Win32_UserAccount -Filter “LocalAccount = $true” | % { $_.Disabled}
    $_ | Select-Object  @{n='UserName';e={$_.Name}},
    @{n='Groups';e={$g1 -join ';'}},
    @{n='LastLogin';e={$_.LastLogin}},
    @{n='DisabledSatus';e={$s1 -join ''}}    
       }

Output:

UserName                        Groups                          LastLogin                       Disabled                        
--------                        ------                          ---------                       -----                         
Administrator                   Administrators                  6/5/2012 5:18:54 PM             OKOKDegradedOKOKOKOKOKOKDeg...
TestUser                Users               10/10/2013 7:11:22 PM       OKOKDegradedOKOKOKOKOKOKDeg..

Upvotes: 0

Views: 516

Answers (1)

Tim Ferrill
Tim Ferrill

Reputation: 1674

Two changes. I'd add this at the top of your ForEach-Object loop:

$currentuser = $_

And change the $s line to this:

$s1 = gwmi -ClassName Win32_UserAccount -Filter “LocalAccount = $true” | ? {$_.Name -eq $currentuser.Name} | % { $_.Disabled}

Upvotes: 1

Related Questions