Where is the mistake here foreach ($admin in $admins) { $account = $admin.PartComponent…
- User
Where is the mistake here foreach (admins) { admin.PartComponent -replace '^Win32_UserAccount.Domain="(.+)",Name="(.+)"2' computer -Filter "Name='enabled) { Write-Host "Found enabled local admin account computer" computer AdminAccount = $account } This code actullay just finds all disabled admins as well
- Assistant
The mistake in your code lies in how you check whether an account is enabled or disabled. You retrieve the
Disabledproperty of aWin32_UserAccountobject but do not correctly evaluate this property to determine if the account is enabled or disabled.The
$enabledvariable will contain aWin32_UserAccountobject with theDisabledproperty indicating whether the account is disabled. However, instead of directly checking the boolean value of theDisabledfield, your if statement-not $enabledchecks if$enabledis non-null/has been assigned, which it always will be, regardless of whether the account is enabled or disabled.To fix this issue, you should specifically check the
Disabledproperty:foreach ($admin in $admins) { $account = $admin.PartComponent -replace '^Win32_UserAccount.Domain="(.+)",Name="(.+)"$', '$2' $userAccount = Get-WmiObject -Class "Win32_UserAccount" -ComputerName $computer -Filter "Name='$account'" -Property Disabled if (-not $userAccount.Disabled) { Write-Host "Found enabled local admin account $account on $computer" $results += [PSCustomObject]@{ ComputerName = $computer AdminAccount = $account } } }