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 2 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
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 »

Now run this:

Code: Select all

$adspath = 'LDAP://' + ([adsisearcher]"samaccountname=$($env:COMPUTERNAME)$").FindOne().Properties['distinguishedname'][0]
$account = [adsi]$adspath 
$account | select *
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 »

newresults.png
newresults.png (73.22 KiB) Viewed 3109 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 »

Notice that "Parent" is clearly listed.

Code: Select all

$adspath = 'LDAP://' + ([adsisearcher]"samaccountname=$($env:COMPUTERNAME)$").FindOne().Properties['distinguishedname'][0]
$account = [adsi]$adspath 
$account | select parent
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 »

Yes but not when I do this
  1. $adspath = ([adsisearcher]"samaccountname=$($env:COMPUTERNAME)$").FindOne().Properties['adspath']
  2. ([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 »

Did you run the code I just posted?
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: Thu Feb 08, 2018 1:43 pm Did you run the code I just posted?
Yes it returns the parent. So I should have went back and revisted this post on Page 2

we're discussing a moot point

we have this down to a one-liner, the current script is 3
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 »

I have no idea what you are referring to. What is the one-liner?

I posted the last script way back in the beginning. It worked then and works now so I do not understand the issue.
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: Thu Feb 08, 2018 1:53 pm I have no idea what you are referring to. What is the one-liner?

I posted the last script way back in the beginning. It worked then and works now so I do not understand the issue.
(([adsisearcher]"samaccountname=$($env:computername)$").FindOne().Properties['distinguishedname']).split(",")[1] -replace ('OU=')

I said on Page 1 this works,would just like it shorter if possible, but it's not a deal breaker.
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 »

So what you wanted was the "CN" of the parent OU and not the parent 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 »

You got me
This topic is 6 years and 2 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