foreach ($admin in $admins) { $account = $admin.PartComponent -replace…
- User
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 }
- Assistant
The issue seems to be with how the
$enabledvariable is being checked in the if statement. Currently, the if statement is checking if$enabledis false, which means it will execute the code block if$enabledis null or empty.To fix this, you should check if the "Disabled" property of the
$enabledobject is false (i.e., the account is not disabled) before adding the admin account to the results. Here is the corrected code snippet:foreach ($admin in $admins) { $account = $admin.PartComponent -replace '^Win32_UserAccount.Domain="(.+)",Name="(.+)"$', '$2' $enabled = Get-WmiObject -Class "Win32_UserAccount" -ComputerName $computer -Filter "Name='$account'" -Property Disabled # Check if the account is not disabled before adding to results if ($enabled -and (-not $enabled.Disabled)) { Write-Host "Found enabled local admin account $account on $computer" $results += [PSCustomObject]@{ ComputerName = $computer AdminAccount = $account } } }With this change, the code will only add admin accounts to the results if they are not disabled. This should solve the issue of finding all admin accounts, regardless of their enabled/disabled status.