Searching local user accounts

dwalters
New Contributor

I am trying to search local user accounts, specifically UID's in the range of 502-505. Is there a way or script I could use to do this?

4 REPLIES 4

mm2270
Legendary Contributor III

What exactly do you mean. Searching via a script run from a policy or Casper Remote? Or do you mean running an inventory report in the JSS?

Anyway, looking up local account UIDs is pretty easy if you meant from a script.
If you really only want the UIDs between 502 and 505, try this-

dscl . list /Users UniqueID | awk '$2 >= 502 && $2 <= 505 {print $1}'

That would include UID 502 and 505. Not sure if you meant greater than and less than or including those. If you meant the former, just remove the 2 = symbols in the above.

If instead you want all local accounts from UID 501 and up, use:

dscl . list /Users UniqueID | awk '$2 > 500 {print $1}'

dwalters
New Contributor

Trying to find users that dont have a manage account. Local users that have managed accounts have a long UID and those that just have a local account are in the 502-505 range. 501 UID is the local admin that we put on with every image.

mm2270
Legendary Contributor III

I see. So then you're using a directory service for your regular user accounts. The long UID numbers you describe are typical of either OD and AD based accounts. I don't recall where OD account UIDs start. I believe AD starts at 1000 and up. You may be better off with a command that will look through everything up to the start of your directory based accounts and excluding 501. But you can do it however you want.

nessts
Valued Contributor II

here is a perl subroutine that checks for local users, and reports true, you could edit the else section and delete the accounts or whatever else if you so desire.

sub checkForLocalUsers { my $rc = 'false'; my $user_dir = '/private/var/db/dslocal/nodes/Default/users'; my $dscl_cmd = 'dscl . -read Users/'; my @userlist; opendir E, $user_dir or die "Users Directory $! "; @userlist = readdir(E); closedir E; foreach my $u (@userlist) { next if $u =~ /^_/; next if $u =~ /^./; (my $uname = $u) =~ s/.plist$//; my $newcmd = $dscl_cmd . $uname . " UniqueID"; my $uid = (split /:/, `$newcmd`)[1]; if ($uid < 501) { next; } else { $rc = 'true'; last; } } return $rc;
}