Using ADSI to get local admins

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
MarvelManiac
Posts: 63
Last visit: Thu Sep 13, 2018 3:40 pm

Using ADSI to get local admins

Post by MarvelManiac »

Hello,
So I've seen this code posted several times around the web
  1. ([ADSI]("WinNT://PCNAME/Administrators,group")).Invoke('Members') |
  2.     ForEach-Object {
  3.         ($_.GetType()).InvokeMember('ADspath', 'GetProperty', $null, $_, $null).Replace('WinNT://', '')
  4.     }
Can anyone explain
Why it takes so long somtimes? Is there a better version without using GWMI? ( we have a current build problem with Get-WmiObject will fail )

I've also had some inconsistencies such as:

Error while invoking GetType. Could not find member.
At line:3 char:9
+ ($_.GetType()).InvokeMember('ADspath', 'GetProperty', $null, ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], MissingMemberException
+ FullyQualifiedErrorId : System.MissingMemberException

There are all lan connected PCs that are in our general OU that should have 3 local admins. I get this error twice,then it will display one of the three local admins, then one more error.

There is nothing else to this script as of now.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Using ADSI to get local admins

Post by jvierra »

I would suspect network or system issues. That code is used extensively. I have never seen it fail.
This topic is 5 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked