Fastest Way to get OU of PC remotely

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 6 years and 1 month 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
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Fastest Way to get OU of PC remotely

Post by localpct »

mxtrinidad wrote: Wed Feb 07, 2018 10:34 am I won't suggest using COMobject as it's old technology, and harder to use. I understand you may not want to use the AD module.
Use .NET System.DirectoryServices which is simpler (in my opinion) to use and you can find plenty of samples.

If you use the AD module is as simple as doing:

(Get-ADComputer "Earth" -Properties *).DistinguishedName.Split(',')

PS C:\Users\Administrator> (Get-ADComputer "Earth" -Properties *).DistinguishedName.Split(',')
CN=EARTH
CN=Computers
DC=universe
DC=trinity
DC=mx

Or, use .NET System.DirectoryServices code:
PS C:\Users\Administrator> $ComputerName = $env:ComputerName; $Domain = $env:USERDNSDOMAIN
PS C:\Users\Administrator> $root = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$($Domain)")
PS C:\Users\Administrator> $searcher = New-Object System.DirectoryServices.DirectorySearcher($root)
PS C:\Users\Administrator> $searcher.Filter = "(&(objectClass=computer)(name=$ComputerName))"
PS C:\Users\Administrator> [System.DirectoryServices.SearchResult]$result = $searcher.FindOne()
PS C:\Users\Administrator> $result.Properties["DistinguishedName"].Split(',')
CN=EARTH
CN=Computers
DC=universe
DC=trinity
DC=mx
PS C:\Users\Administrator>

Hope this helps you!
It gave me some insight into the bigger picture of ADSI and realizing I’m soooo far behind in this field that I think I’ll bring a unique speciality at my company.

Thanks so much!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Fastest Way to get OU of PC remotely

Post by jvierra »

Every WIndows tech needs to learn ADSI. You cannot work with AD in script without the knowledge except if you plan to only use AD CmdLets.

This code was never tested by you even though you said you could get adspath. "adspath" is not an attribute in ADSI.

Code: Select all

$adspath = ([adsisearcher]"samaccountname=$($env:COMPUTERNAME)$").FindOne().Properties['adspath']
([adsi]$adspath).Parent
This is the only method that works. The "parent" attribute is also only available on the full object which I forgot.

Code: Select all

$adspath = 'LDAP://' + ([adsisearcher]"samaccountname=$($env:COMPUTERNAME)$").FindOne().Properties['distinguishedname'][0]
([adsi]$adspath).Parent
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Fastest Way to get OU of PC remotely

Post by jvierra »

You should also be able to do this:

([adsisearcher]"samaccountname=$($env:COMPUTERNAME)$").FindOne().Properties['ou']
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Fastest Way to get OU of PC remotely

Post by localpct »

jvierra wrote: Wed Feb 07, 2018 9:41 pm
This code was never tested by you even though you said you could get adspath. "adspath" is not an attribute in ADSI.

Code: Select all

$adspath = ([adsisearcher]"samaccountname=$($env:COMPUTERNAME)$").FindOne().Properties['adspath']
([adsi]$adspath).Parent
This is the only method that works. The "parent" attribute is also only available on the full object which I forgot.
Just wanted to clear the air real quick.

Are you saying I didn’t test a script that you posted but then said it didn’t work?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Fastest Way to get OU of PC remotely

Post by jvierra »

You said you tested something and could get "adspath" and "distinguishedname" but not "parent"

Not important. It just confused me a bit.
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Fastest Way to get OU of PC remotely

Post by localpct »

I’ll show you how I came to that later

Again, if I could point at the screen
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Fastest Way to get OU of PC remotely

Post by jvierra »

The issue is around your stating that something doesn't work without saying what didn't work. That is as good as pointing at the screen. We need clear descriptions and full error messages.
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Fastest Way to get OU of PC remotely

Post by localpct »

$adspath = ([adsisearcher]"samaccountname=$($env:COMPUTERNAME)$").FindOne().Properties['adspath']
([adsi]$adspath).Parent

This is what that returns :D

Cannot convert value "System.DirectoryServices.ResultPropertyValueCollection" to type "System.DirectoryServices.DirectoryEntry". Error: "The value provided for adsObject does not implement IADs."
At line:2 char:1
+ ([adsi]$adspath).Parent
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastConstructorException


([adsisearcher]"samaccountname=$($env:COMPUTERNAME)$").FindOne().Properties['ou']
This returns nothing

If I type this

([adsisearcher]"samaccountname=$($env:COMPUTERNAME)$").FindOne().Properties

It list different properties on my PC and Parent is not one
Properties.png
Properties.png (49.48 KiB) Viewed 3366 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Fastest Way to get OU of PC remotely

Post by jvierra »

What is the output of this:

Code: Select all

$adspath = 'LDAP://' + ([adsisearcher]"samaccountname=$($env:COMPUTERNAME)$").FindOne().Properties['distinguishedname'][0]
[adsi]$adspath
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Fastest Way to get OU of PC remotely

Post by localpct »

output.png
output.png (10.93 KiB) Viewed 3356 times
This topic is 6 years and 1 month 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